# Apply a transformation to one sublist of a list

Say that I have a list of the form

A={1,2,{{3,4},{5,6}}}


I want to manipulate the first elements of the last sublist. For example, in the case of multiplication by two, the resulting matrix should become

B={1,2,{{6,4},{10,6}}}


In my case, the third sublist contains around 40,000 pairs of numbers. If possible, I would like to avoid iterating through each data point. Any help would be greatly appreciated.

• B = A; B[[-1, All, 1]] *= 2;, related: 42455 – Kuba Jun 15 '19 at 12:54
• Have you looked at SubsetMap? – Carl Lange Jun 15 '19 at 13:06

You can use Map. Suppose f is the function you want to apply to the latter part of the list:

a = {1, 2, {{3, 4}, {5, 6}}};
Map[f, a, {2}]
{1, 2, {f[{3, 4}], f[{5, 6}]}}


For your particular f (multiplication by 2)

f[{x_, y_}] := {2 x, y};
Map[f, a, {2}]
{1, 2, {{6, 4}, {10, 6}}}

• Tweak that definition of f slightly to match his example output – Bill Jun 15 '19 at 20:06
• @Bill -- It's now tweaked... – bill s Jun 16 '19 at 4:46