Suppose I have a list as the following:
list={{1,9},{7,0},{8,6}....}
How can I reverse the element in such way that the output would be the following:
{{9,1},{0,7},{6,8}....}
I tried Map[Reverse,{list}]
but it didn't work.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitySuppose I have a list as the following:
list={{1,9},{7,0},{8,6}....}
How can I reverse the element in such way that the output would be the following:
{{9,1},{0,7},{6,8}....}
I tried Map[Reverse,{list}]
but it didn't work.
Reverse
takes a level specification so:
Reverse[list, 2]
Another approach would be to use a simple pattern match and replacement rule, such as:
list = {{1, 9}, {7, 0}, {8, 6}};
list /. {a_?NumberQ, b_?NumberQ} :> {b, a}
(* {{9, 1}, {0, 7}, {6, 8}}. *)
For this specific example, you could drop the ?NumberQs from the pattern to be matched. But, without them, the approach would not produce the intended outcome if the length of the outer list were 2. For that case, the outcome would be as follows:
{{1, 9}, {7, 0}} /. {a_, b_} :> {b, a}
(*. {{7, 0}, {1, 9}}. *)
list[[All, {2,1}]]
, or more generallylist[[All, Reverse[Range[Length[First[list]]]]]]
$\endgroup$ – Coolwater Feb 3 '16 at 20:12{}
's:Map[Reverse, list]
orReverse /@ map
. $\endgroup$ – march Feb 3 '16 at 20:38