# Operating on the first element of each sublist only

I have the following list containing 2-element sublists:

list={{0.01, 0.037348}, {0.03, 0.165434}, {0.1, 0.263921}, {0.3,
0.560191}, {1., 0.968857}, {3., 1.50965}, {10., 2.36502}, {30.,
3.07659}, {100., 3.73412}, {300., 4.4931}, {1000., 5.06818}, {3000.,
5.65423}, {10000., 6.00944}}

How can I get the natural logarithm (Log[]) of the first element of each sublist (e.g. 0.01, 0.03.....10000) only while leaving the second one intact?. I would like this operation to apply to other sublists of different lengths.

• {Log[First[#]],Last[#]}&/@list will make a new list with log of the first element. Jun 28, 2020 at 1:26
• This works great! @flinty. Thank you very much !!!
– John
Jun 28, 2020 at 1:29
• Or {Log@#1, #2} & @@@ list. There are many similar questions and this will be signed as a duplicate. Jun 28, 2020 at 1:50
• list[[All,1]]=Log@list[[All,1]] Jun 28, 2020 at 2:33
• If you're ultimately planning to plot this data, you may want to look at the documentation for ListLogPlot, ListLogLinearPlot, and/or ListLogLogPlot. Jun 28, 2020 at 14:04

This should be fast for large lists:

Transpose[{Log @ #, #2} & @@ Transpose[#]] & @ list
{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191},
{0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517,  3.73412},
{5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

You may use MapAt.

MapAt[Log, {All, 1}]@list
{{-4.60517, 0.037348}, {-3.50656, 0.165434},
{-2.30259, 0.263921}, {-1.20397, 0.560191},
{0., 0.968857}, {1.09861, 1.50965},
{2.30259, 2.36502}, {3.4012, 3.07659},
{4.60517, 3.73412}, {5.70378, 4.4931},
{6.90776, 5.06818}, {8.00637, 5.65423},
{9.21034, 6.00944}}

Hope this helps.

In addition, in v 12.0 and greater, you may use SubsetMap

SubsetMap[Log, list, {All,1}]

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

newlist=Table[{Log[list[[k]]],list[[k]]},{k,1,Length[list]}]

is inelegant but will work.

Query[All, {1 -> Log}] @ list

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}

Map Log over first and Log10 over last column:

Query[All, {1 -> Log, -1 -> Log10}] @ list;

To Query rows omit the All:

Query[{1 -> Log, -1 -> Log10}] @ list
list={{0.01, 0.037348}, {0.03, 0.165434}, {0.1, 0.263921}, {0.3,
0.560191}, {1., 0.968857}, {3., 1.50965}, {10., 2.36502}, {30.,
3.07659}, {100., 3.73412}, {300., 4.4931}, {1000., 5.06818}, {3000.,
5.65423}, {10000., 6.00944}}

Cases[list, {a_, b_} :> {Log[a], b}, 1]
Replace[list, {a_, b_} :> {Log[a], b}, {1}]
SequenceReplace[list, {{a_, b_}} :> {Log[a], b}]
{list[[All, 1]] // Log, list[[All, 2]]} // Transpose
MapThread[Compose, {{Log, Identity}, #}] & /@ list

{{-4.60517, 0.037348}, {-3.50656, 0.165434}, {-2.30259, 0.263921}, {-1.20397, 0.560191}, {0., 0.968857}, {1.09861, 1.50965}, {2.30259, 2.36502}, {3.4012, 3.07659}, {4.60517, 3.73412}, {5.70378, 4.4931}, {6.90776, 5.06818}, {8.00637, 5.65423}, {9.21034, 6.00944}}