This question already has an answer here:
- Let's do this list-rearrangement prettier [duplicate] 1 answer
- Combining values in one column (or part) when values in another column (or part) match 7 answers
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?