How to sort a list of sublists by the total number of elements in Python

1 Answer

0 votes
lst = [['a', 'b', 'c'], ['d', 'e', 'f', 'g'], ['o'], ['i', 'j', 'k', 'l', 'm'], ['n', 'p'], ['h']]

lst.sort(key = len)

print(lst)
 
 
 
 
'''
run:
 
[['o'], ['h'], ['n', 'p'], ['a', 'b', 'c'], ['d', 'e', 'f', 'g'], ['i', 'j', 'k', 'l', 'm']]

'''

 



answered Feb 21, 2023 by avibootz
...