Reputation: 4113
Supposed I have a list of tuples:
my_list = [(1, 4), (3, 0), (6, 2), (3, 8)]
How do I sort this list by the minimum value in the tuple, regardless of position? My final list will be as follows:
my_sorted_list = [(3, 0), (1, 4), (6, 2), (3, 8)]
Upvotes: 2
Views: 1829
Reputation: 353359
You can take advantage of the key
parameter, to either .sort
or sorted
:
>>> my_list = [(1, 4), (3, 0), (6, 2), (3, 8)]
>>> sorted(my_list, key=min)
[(3, 0), (1, 4), (6, 2), (3, 8)]
Upvotes: 8