Map and Apply a function on a nested list
Since you seem to be relatively new to Mathematica, and unfamiliar with all its special syntax (/@
, @@@
etc), I would normally recommend Artes' second answer:
{#1, Log[#2]} & @@@ {{1, 2}, {4, 2}, {6, 4}}
Which can also be written
Apply[{#1, Log[#2]} &, testdata, {1}]
where testdata = {{1, 2}, {4, 2}, {6, 4}}
Some alternative ways of getting the same answer include:
MapThread[{#1, Log[#2]} &, Transpose@testdata]
and (I think this one is quite cool)
Inner[#1[#2] &, {# &, Log[#] &}, Transpose@testdata, List]
MapAt
and deeply nested lists generalization
Another way to do this:
MapAt[Log, #, 2] & /@ {{1,2}, {4,2}, {6,4}}
{{1, Log[2]}, {4, Log[2]}, {6, Log[4]}}
Which is useful if we target a specific element inside every element of a deeply nested list:
data = Table[{k, {k, {{k}}}}, {k, 2, 5}]
{{2, {2, {{2}}}}, {3, {3, {{3}}}}, {4, {4, {{4}}}}, {5, {5, {{5}}}}}
MapAt[Log, #, {2, 2}] & /@ data
{{2,{2,{{Log[2]}}}}, {3,{3,{{Log[3]}}}}, {4,{4,{{Log[4]}}}}, {5,{5,{{Log[5]}}}}}
You can use ReplaceAll
i.e.
{{1, 2}, {4, 2}, {6, 4}} /. {a_, b_} -> {a, Log[b]}
{{1, Log[2]}, {4, Log[2]}, {6, Log[4]}}
or
{#1, Log[#2]} & @@@ {{1, 2}, {4, 2}, {6, 4}}
i.e. Apply
the function {#1, Log[#2]} &
on the first level of the expression.