# How to apply a function to a list of tuples

trialList = {{0, 24.701}, {1, 24.69}, {2, 24.733}};
f[x_] := x + 1;


I would like to apply the function f to only the second element of each tuple. How is this done?

The actual list is 16,000 entries long. How do I iterate over the whole list applying the function to only the second entry of each tuple? I tried the following, but it does not do what I want:

{trialList[[1]], f[trialList[[2]]]} & @@@ trialList


If you need to worry about execution speed, then there's a better alternative, using Transpose. First I'll list the timings for the solutions in the other answers. For that purpose, I create a trialList with 160000 random entries, and in the tests I suppress the output to avoid huge output lists:

trialList = RandomReal[{-1, 1}, {160000, 2}];
f[x_] := x + 1

Timing[{#1, f[#2]} & @@@ trialList;]


{0.326740, Null}

Timing[MapAt[f, trialList, {;; , 2}];]


{0.271610, Null}

Timing[trialList[[All, 2]] = f /@ trialList[[All, 2]];]


{0.207944, Null}

Timing[trialList /. {a_, b_} :> {a, f[b]};]


{0.290535, Null}

Here is my proposal:

Timing[Transpose[{#1, Thread[f[#2]]} & @@ Transpose[trialList]];]


{0.053719, Null}

It's 4 to 6 times faster. The right-most Transpose converts the trialList into a list with only two entries, each of which has 160000 elements. The first list contains all the first members of the original tuples, and the second list the second members. The @@ to the left of Transpose says to Apply to this list a function which is {#1, Thread[f[#2]]} &. It has two formal arguments #1 and #2, corresponding to the two large lists. Only the second argument is modified by Threading your function f over it (i.e., applying it to all the elements of the list #2). The result is two lists, of which only the second differs from the original. By doing another Transpose we get the original tuple structure back.

trialList = {{0, 24.701}, {1, 24.69}, {2, 24.733}};
f[x_] := x + 1


You can also use

MapAt[f, trialList, {;; , 2}] (* Version 9 *)
MapAt[f, trialList, {#, 2} & /@ Range[Length[trialList]]] (*Versions 7 & 8. Thanks: Jens *)


or

trialList[[All, 2]] = f /@ trialList[[All, 2]]; trialList


or

trialList /. {a_, b_} :> {a, f[b]}


all give:

(* {{0,25.701},{1,25.69},{2,25.733}} *)


Update: A combination of MapAt and Transpose

Transpose@MapAt[f, Transpose[trialList], {2}]


(Timings approximately same as Jens' Transpose[{#1, Thread[f[#2]]} & @@ Transpose[trialList]])

• The MapAt solution works in version 9, but in version 8 it throws an error "Position specification {1;;All,2} ...is not an integer or a list of integers"
– Jens
Mar 23, 2013 at 5:52
• @Jens, thank you. Interestingly, the Doc Center entry on MapAt has not changed since, at least, Version 7: MapAt[f,expr,{i,j,...}] or Map[f,expr,{{i,j,...}}] applies f to the part expr[[i,j,...]]] ... but it works as advertised/expected only in Version 9.
– kglr
Mar 23, 2013 at 6:22

You can use

{#1, f[#2]}& @@@ trialList


Use it exactly as I wrote it, do not change #1 and #2.

f @@@ list is a shorthand for Apply[f, list, {1}]. To understand what it does, try g @@@ {{1,2}, {3,4}, {5,6}} with an inert (undefined) g. You'll get {g[1,2], g[3,4], g[5,6]}.

& is used to construct a pure function. Pure functions are explained here.

• It worked. How do you use the '&' and '@@@' to solve the problem? Like what are they doing? Mar 23, 2013 at 5:13
• @user2200667 I added links to the appropriate doc pages, see the edit please. Mar 23, 2013 at 14:40

A few years ago I posted a solution on another forum, which I will repeat here. It occurred to me that MapAt would be the correct builtin function if it either allowed the use of All or accepted a level specification, neither of which were valid in version 8, so I wrote my own version

MapAtLevel[f_, expr_, n_, levelspec_: {1}] :=
Map[MapAt[f, #, n] &, expr, levelspec - 1];

list = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
MapAtLevel[f, list, 3, {2}]


yields

{{1, 2, f[3]}, {4, 5, f[6]}, {7, 8, f[9]}}


This post got me interested in the problem again, so on a lark I tried the All approach in version 9

MapAt[f, list, {All, 3}]


yields

{{1, 2, f[3]}, {4, 5, f[6]}, {7, 8, f[9]}}


So, the behavior of MapAt` has changed as of version 9, although the Doc Center has not been updated as Jens points out above. So, yet another utility function I have written to deal with Mathematica quirks has been made obsolete, and I could not be happier. Thanks for reading the forums and responding, Wolfram developers.