6

Let's say I have some data:

data={{a1,b1},{a2,b2},{a3,b3},{a4,b4},...}

and I would like to apply the function:

f1[x_]=x*5 to every a_i element.

How can I do this?

The best solution I've found so far is to do:

MapAt[f1, data, {{1, 1}, {2, 1}, {3, 1}, {4, 1}...}]

Up to the length of my data, but I'm sure there's a more efficient solution for this.

Kuba
  • 136,707
  • 13
  • 279
  • 740
user5615
  • 267
  • 1
  • 8

1 Answers1

8

There are a number of ways (if I understand aim),e.g.:

lst = data = {{a1, b1}, {a2, b2}, {a3, b3}, {a4, b4}};
lst /. {x_, y_} :> {f[x], y}
MapAt[f, lst, {All, 1}]
{f@#1, #2} & @@@ lst
ubpdqn
  • 60,617
  • 3
  • 59
  • 148
  • Whenever I do one of these solutions I get the resulting list $ {{a1,{b1}},{a2,{b2}},etc.}}$. How do I remove the resulting list around the second element? – Ely Eastman Apr 24 '20 at 14:48
  • @ElyEastman what input are you using. The code applies function to first part of each element – ubpdqn Apr 25 '20 at 04:48
  • I realized I was using an incorrect input for my function. It was dividing by a list element, which would encapsulate bi in a list. – Ely Eastman Apr 26 '20 at 00:06