
This is a tutorial on how to sort a list of tuples on any column, in ascending or descending order. This works for list of lists as well.
Create an example list of tuples
We will show how to sort a list of tuples using an example.
This table represents the most populous cities in the world. It also includes the country amd population. Right now, it is sorted according to the population, in descending order.
| City | Country | Population |
|---|---|---|
| Tokyo | Japan | 36,953,600 |
| Delhi | India | 35,518,400 |
| Shanghai | China | 31,049,800 |
| Dhaka | Bangladesh | 25,359,100 |
| Cairo | Egypt | 23,534,600 |
| São Paulo | Brazil | 23,168,700 |
| Mexico City | Mexico | 23,016,800 |
| Beijing | China | 22,983,400 |
| Mumbai | India | 22,539,300 |
| Osaka | Japan | 18,873,900 |
We will create a list of tuples from this table. For that, we will use this semicolon-separated multiline string.
s = '''Tokyo;Japan;36_953_600
Delhi;India;35_518_400
Shanghai;China;31_049_800
Dhaka;Bangladesh;25_359_100
Cairo;Egypt;23_534_600
São Paulo;Brazil;23_168_700
Mexico City;Mexico;23_016_800
Beijing;China;22_983_400
Mumbai;India;22_539_300
Osaka;Japan;18_873_900
'''
lines = [x.split(';') for x in s.splitlines()] # create a list of lists
lines = [(line[0], line[1], int(line[2])) for line in lines] # create a list of tuples with int on population
print(lines)
Output:
[('Tokyo', 'Japan', 36953600), ('Delhi', 'India', 35518400), ('Shanghai', 'China', 31049800), ('Dhaka', 'Bangladesh', 25359100), ('Cairo', 'Egypt', 23534600), ('São Paulo', 'Brazil', 23168700), ('Mexico City', 'Mexico', 23016800), ('Beijing', 'China', 22983400), ('Mumbai', 'India', 22539300), ('Osaka', 'Japan', 18873900)]
Sorting the list of tuples by city in ascending order
We can sort using either sorted() which returns a new list or in-place using list.sort()
The city field is the first field, so we will reference to it with index 0. We will use lambda to do the sorting based on index 0.
Sort the list of tuples by the first field in ascending order using sorted()
slines = sorted(lines, key=lambda x: x[0])
print(slines)
Output:
[('Beijing', 'China', 22983400), ('Cairo', 'Egypt', 23534600), ('Delhi', 'India', 35518400), ('Dhaka', 'Bangladesh', 25359100), ('Mexico City', 'Mexico', 23016800), ('Mumbai', 'India', 22539300), ('Osaka', 'Japan', 18873900), ('Shanghai', 'China', 31049800), ('São Paulo', 'Brazil', 23168700), ('Tokyo', 'Japan', 36953600)]
Sort the list of tuples by the first field in ascending order in-place using sort()
Similar to the previous section, we will pass in lambda with the first index. We use the key parameter and pass an anoynmous lambda on it using the first column, hence we have lambda x:x[0].
lines.sort(key=lambda x:x[0])
print(lines)
Output:
[('Beijing', 'China', 22983400), ('Cairo', 'Egypt', 23534600), ('Delhi', 'India', 35518400), ('Dhaka', 'Bangladesh', 25359100), ('Mexico City', 'Mexico', 23016800), ('Mumbai', 'India', 22539300), ('Osaka', 'Japan', 18873900), ('Shanghai', 'China', 31049800), ('São Paulo', 'Brazil', 23168700), ('Tokyo', 'Japan', 36953600)]
Sorting the list of tuples by any field in ascending and descending order
To sort the list in descending order, pass in the parameter reverse=True.
To sort the list in ascending order, pass in the parameter reverse=False.
Sort the list of tuples by the first field in ascending order
slines = sorted(lines, key=lambda x: x[0], reverse=False)
print(slines)
Output:
[('Beijing', 'China', 22983400), ('Cairo', 'Egypt', 23534600), ('Delhi', 'India', 35518400), ('Dhaka', 'Bangladesh', 25359100), ('Mexico City', 'Mexico', 23016800), ('Mumbai', 'India', 22539300), ('Osaka', 'Japan', 18873900), ('Shanghai', 'China', 31049800), ('São Paulo', 'Brazil', 23168700), ('Tokyo', 'Japan', 36953600)]
Sort the list of tuples by the last field (population) in ascending order in-place
lines.sort(key=lambda x: x[2], reverse=False)
print(lines)
Output:
[('Osaka', 'Japan', 18873900), ('Mumbai', 'India', 22539300), ('Beijing', 'China', 22983400), ('Mexico City', 'Mexico', 23016800), ('São Paulo', 'Brazil', 23168700), ('Cairo', 'Egypt', 23534600), ('Dhaka', 'Bangladesh', 25359100), ('Shanghai', 'China', 31049800), ('Delhi', 'India', 35518400), ('Tokyo', 'Japan', 36953600)]
Sort the list of tuples by the second field (country) in descending order
slines = sorted(lines, key=lambda x: x[1], reverse=False)
print(slines)
Output:
[('Dhaka', 'Bangladesh', 25359100), ('São Paulo', 'Brazil', 23168700), ('Beijing', 'China', 22983400), ('Shanghai', 'China', 31049800), ('Cairo', 'Egypt', 23534600), ('Mumbai', 'India', 22539300), ('Delhi', 'India', 35518400), ('Osaka', 'Japan', 18873900), ('Tokyo', 'Japan', 36953600), ('Mexico City', 'Mexico', 23016800)]
Sort the list of tuples by the second field (country) in descending order in-place
lines.sort(key=lambda x: x[1], reverse=False)
print(lines)
Output:
[('Dhaka', 'Bangladesh', 25359100), ('São Paulo', 'Brazil', 23168700), ('Beijing', 'China', 22983400), ('Shanghai', 'China', 31049800), ('Cairo', 'Egypt', 23534600), ('Mumbai', 'India', 22539300), ('Delhi', 'India', 35518400), ('Osaka', 'Japan', 18873900), ('Tokyo', 'Japan', 36953600), ('Mexico City', 'Mexico', 23016800)]
Conclusion
Hope this solution worked for you. If it did or did not, please feel free to comment below. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.