How to sort a list based on the length of the string of each element in Python

1 Answer

0 votes
lst = ['python', 'c', 'c++', 'javascript', 'java', "c#", "swift", "nodejs"]

lst.sort(key = len)

print(lst)



'''
run:

['c', 'c#', 'c++', 'java', 'swift', 'python', 'nodejs', 'javascript']

'''

 



answered Apr 19, 2021 by avibootz
...