Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an list:

l = {{4,5},{7,8},{9,5},{3,2},{1,2},{13,12}}. 

I want to take the elements in the list in the sequential order. For example the first element in the list is {4,5} and I want to select the first member in the sub-list, 4, then I want to go to the second element in the list {7,8} and to take the first element 7. This process continues up to {13,12} and selecting 13. How can I do this?

share|improve this question
{{4,5},{7,8},{9,5},{3,2},{1,2},{13,12}}[[All,1]] ? – b.gatessucks Jul 6 '12 at 14:19
Either First /@ list or First[Transpose[list]] would be the ticket... – J. M. Jul 6 '12 at 14:19
Take a look at Part in the documentation, it is something you'll use a lot: – VLC Jul 6 '12 at 14:21
Where you are getting confused is with the idea that you need to do this "one by one". Mathematica does not need to do element-by-element arithmetic on lists "one by one" as if looping over the list. See this question for some useful parts of the Mathematica language that are more efficient and faster to write than the procedural way you are thinking about it now. – Verbeia Jul 6 '12 at 20:10
@mr.wizard I wouldn't call this a moving target because the OP edited it fairly quickly after posting and all but one answer addresses the modified question. By editing, you've rendered the other answers kinda silly. Such minor additions are better off in the same question. Granted, if it were me, I would notify acl to let him know of the change, but that's about it. Moving goal posts would imply repeated editing to change the scope once the answers have been given to try and squeeze more out of a single question, which is not the case here. – R. M. Jul 7 '12 at 14:23
up vote 3 down vote accepted

You want to take the first number if sublist #1 and add a random number between 1 and 10 to it (not specified whether this should be an integer or real), then take the first element of sublist #2 and add a random number -- in this case the range is not specified so I am assuming this is also between 1 and 10 ...and so on.

Mathematica works better when you work on entire lists rather than thinking about doing things element by element.

l = {{4,5},{7,8},{9,5},{3,2},{1,2},{13,12}}

So take all the first numbers using Part:


and add a list of random numbers to it using Plus

l[[All, 1]] + RandomReal[{1, 10}, Length[l]]

So in other words you simply add a list of random numbers to your list of first numbers. Depending on how to interpret your question this could be the end of it because a random number has been added to all the first numbers. However if you wanted to actually modify your original list then

l[[All, 1]] = l[[All, 1]] + RandomReal[{1, 10}, Length[l]]

We can have look at the newly modified list:

share|improve this answer

For instance,

lst = {{4, 5}, {7, 8}, {9, 5}, {3, 2}, {1, 2}, {13, 12}};
lst[[All, 1]]

{4, 7, 9, 3, 1, 13}
share|improve this answer

Another example using Apply at level 1 or @@@:

lst = {{4, 5}, {7, 8}, {9, 5}, {3, 2}, {1, 2}, {13, 12}};
#1 + RandomReal[{1, 10}] & @@@ lst

This addresses your question of selecting elements one-by-one and adding a random number between 1 and 10 to it. If, as the title suggests, all you want to do is to pick the first element one-by-one, then you only need #1 & @@@ lst.

Note that "vectorized" approaches will be far faster, and as Verbeia notes, you should exploit the Listable attribute of several built-ins. One such solution would be:

# + RandomReal[{1, 10}, Length@#] &@First@Transpose@lst
share|improve this answer
Clarification for beginners #1 & @@@ lst – Dr. belisarius Jul 6 '12 at 16:46

Also, for example:

lst = {{4, 5}, {7, 8}, {9, 5}, {3, 2}, {1, 2}, {13, 12}};


Take[Transpose@lst, 1]

lst /. {a_, _} -> a

These will all return the first column of lst, which you can add to a vector of random numbers as desired. You can create that random vector using RandomInteger[{1, 10}, Length[lst]].

share|improve this answer
l = {{4,5},{7,8},{9,5},{3,2},{1,2},{13,12}};
l[[#]] + {RandomInteger[{1,10}], 0} & /@ Range[Length[l]]

This should return the initial list 'l' with the first sub element of each element modified by addition of a random Integer between 1 and 10. The output looks like:

{{11, 5}, {16, 8}, {19, 5}, {9, 2}, {8, 2}, {15, 12}}
share|improve this answer
This gives the correct output but there is no need to map the function part by part. Addition works element-by-element on lists (vectors, matrices, tensors). – Verbeia Jul 6 '12 at 21:10

You question isnt entirely clear, but if what you want to do is get the whole matrix with random numbers added to the first column, then this is what you want:

lst + Transpose[{RandomInteger[{1,10},Length[lst]],ConstantArray[0,Length[lst]]}]

It would be worth looking up in the documentation about Listable functions. Mathematica can do arithmetic on vectors, matrices and tensors directly without having to build up the calculation part by part. So while the answer of Alex gives the correct output, it is a very roundabout way to do it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.