2

Consider a list (list) as

list = {{1, 3}, {1, 4}, {1, 5}, {1, 6}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {3, 
  3}, {3, 4}, {3, 5}, {3, 6}, {4, 3}, {4, 4}, {4, 5}, {4, 6}, {5, 
  2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {6, 2}, {6, 3}, {6, 4}, {6, 
  5}, {6, 6}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {8, 3}, {8, 4}, {8, 
  5}, {8, 6}};

I want to reshape this list into the following form

listTransformed = {{{1, 3}, {1, 4}, {1, 5}, {1, 6}}, {{2, 3}, {2, 4}, {2, 
5}, {2, 6}}, {{3, 3}, {3, 4}, {3, 5}, {3, 6}}, {{4, 3}, {4, 4}, {4, 5}, {4,
6}}, {{5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}}, {{6, 2}, {6, 
3}, {6, 4}, {6, 5}, {6, 6}}, {{7, 3}, {7, 4}, {7, 5}, {7, 6}}, {{8,
3}, {8, 4}, {8, 5}, {8, 6}}};

The pattern is that the sublists are grouped together if their first elements are the same.

How can I do this?

user36426
  • 3,335
  • 11
  • 29

1 Answers1

3
listTransformed = GatherBy[list, First]
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108