How to sort a list of tuples by the second value in Python

1 Answer

0 votes
lst = [('eee', 3),('aaa', 7),('ddd', 1),('bbb', 4),('ccc', 2)]

# x: x[1] = ignore the first value = by the second value
lst = sorted(lst, key=lambda x: x[1])

print(lst)
 
 
 

 
'''
run:
 
[('ddd', 1), ('ccc', 2), ('eee', 3), ('bbb', 4), ('aaa', 7)]
 
'''

 



answered Apr 28, 2021 by avibootz
...