How to Reverse Subsublist Elements in Mathematica?

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on reversing the elements of subsublists in Mathematica, specifically using the list structure lis= {{{2, 1}, {3, 2}, {8, 2}}, {{9, 6}, {1, 0}, {0, 2}}}. The user initially attempted to use Map[Reverse, Flatten[lis, 1]], which resulted in a flattened list that lost the original sublist structure. The correct approach is to utilize Map[Reverse, lis, {2}], which effectively reverses the elements while preserving the nested list format.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of list structures in Mathematica
  • Knowledge of the Map function and its depth parameter
  • Basic understanding of list manipulation techniques in functional programming
NEXT STEPS
  • Explore advanced list manipulation functions in Mathematica
  • Learn about the Flatten function and its options in Mathematica
  • Investigate other methods for preserving list structures during transformations
  • Review functional programming concepts relevant to Mathematica
USEFUL FOR

Mathematica users, data scientists, and programmers looking to manipulate nested lists effectively while maintaining their structure.

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 ·
Replies
22
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K