# Using Do loop to swap each element in a list

I am currently studying the functional programming part of Mathematica. But I am very confused right now about what I have learned so far.

This because I saw an example in textbook using Do loop to swap list elements.

For example, original list :

lis = {{a, 1}, {b, 2}, {c, 3}}


Desired Result should be:

{{1, a}, {2, b}, {3, c}}


I try to write the program code as below, but no error, no result in the end.

 lis = {{a, 1}, {b, 2}, {c, 3}};

Do[temp[i] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

temp


For fixing this issue, I tried to rewirte line two as:

 Do[temp[[i]] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];


But got errors as below which I also don't know why:

Set::noval: Symbol temp in part assignment does not have an immediate value.

General::stop: Further output of Set::write will be suppressed during this calculation.

So really appreciate anyone could clarify such problem. Thanks a ton!

• Why not Map[Reverse, lis] ? Commented Jun 27, 2017 at 18:49
• because I follow the example from the textbook, so I got some further questions about this example. Commented Jun 27, 2017 at 18:54
• you would need to "initialize" temp as a list to use this approach, eg put temp = {0, 0, 0} or temp=ConstantArray[0,Length@lis] before the Do loop. Commented Jun 27, 2017 at 18:57
• I would not call this method 'functional'. See: Reverse element in nested list
– Kuba
Commented Jun 27, 2017 at 19:00

Two small errors in your code...

 lis = {{a, 1}, {b, 2}, {c, 3}};

Do[temp[i] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

temp


You need to initialize temp to the right size, and then it should be temp[[i]] not temp[i].

temp = lis = {{a, 1}, {b, 2}, {c, 3}};

Do[temp[[i]] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

temp

(*    {{1, a}, {2, b}, {3, c}}    *)


I am guessing that this is the "before" example in your functional programming text?

• Thanks for pointing out, I can understand the initialize process now. But why not temp[i] is still a confusion Commented Jun 27, 2017 at 21:47
• The only one which answers the question so far :)
– Kuba
Commented Jun 28, 2017 at 6:19
• cj, think of temp[i] as a call of the function temp[ ] with argument i, as opposed to temp[[i]] which referencing the ith element of a list temp. Your error that you didn't understand came about because temp needed to be initialized. @Kuba, sometimes folks are too clever by half. :) Commented Jun 28, 2017 at 13:23

Try

Table[{lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length@lis}]


{{1, a}, {2, b}, {3, c}}

Or - a little bit more advanced

Map[Reverse, lis]


{{1, a}, {2, b}, {3, c}}

If you want to do this with Do:

rev = {};
Do[AppendTo[rev, {lis[[i, 2]], lis[[i, 1 ]]}], {i, 1, Length@lis}];

rev


{{1, a}, {2, b}, {3, c}}

The latter becomes rather slow with large lists.

• Surely, if you were use Table, it would be Table[Reverse @ i, {i, lis}] and not the monster you show above. Commented Jun 27, 2017 at 22:15

Just some other ways:

{#2, #1} & @@@ lis


or

Transpose@*Reverse@*Transpose@lis

Reverse[{{1, a}, {2, b}, {3, c}},2]

Try: Do[AppendTo[newlis, Reverse[lis[[i]]]], {i, 1, 3}]
newlis starts as an empty list,newlis={}