Mathematica How to Reverse Subsublist Elements in Mathematica?

  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
To reverse the elements of subsublists in Mathematica while maintaining the original list structure, use the function Map with a specified depth. The command Map[Reverse, lis, {2}] effectively reverses each subsublist without altering the overall list format. This approach ensures that the brackets remain intact, preserving the hierarchy of the original list. The initial attempt using Flatten resulted in a loss of the sublist structure, highlighting the importance of depth specification in the Map function. Utilizing the correct depth allows for targeted manipulation of nested lists in Mathematica.
Sarah rob
Messages
16
Reaction score
0
lis= {{{2, 1}, {3, 2}, {8, 2}}, {{9, 6}, {1, 0}, {0, 2}}}

I am trying to reverse the subsublist so {2,1} becomes {1,2}, {3,2} becomes {2,3}...

I have tried Map[Reverse, Flatten[lis, 1]] but this creates
{{1,2},{2,3},{2,8},{6,9},{0,1},{2,0}}
which erases my sublist

Is there a way I can reverse the subsublist and keep all the brakets intact?
 
Physics news on Phys.org
You can specify the depth at which you want Map to act. So try

Map[Reverse, lis, {2}]
 

Similar threads

Replies
22
Views
3K
Replies
2
Views
3K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
2
Views
3K
Back
Top