I was wondering how can I make python order my collection of tuples so that first similar items would appear grouped and groups ordered by first item.
order group
3 1
4 2
2 2
1 1
After sort
order group
1 1
3 1
2 2
4 2
Python list
unordered = [(3, 1), (4, 2), (2, 2), (1, 1)]
I assume you meant
unordered = [(3, 1), (4, 2), (2, 2), (1, 1)]because that part of your example as you typed it is incompatible with the other two, right?If so, then
or similarly
unordered.sort(key=operator.itemgetter(1,0))if you wanted to sort in place (which given the variable name I’m pretty sure you don’t — it would be seriously weird to name a variable “unordered” if it’s meant to be ordered!-).