How to sort a list of tuples using lambda in Python

1 Answer

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

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


print(lst)
 
 
 

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

 



answered Apr 28, 2021 by avibootz
...