0
List iteration
How do I iterate through a list and return its various elements from the string with most alphabets to the least. Example. L=["television","fan","ant","horse","mode"] Returns ["television","horse","mode","ant""fan"]
5 Answers
+ 3
L = ["television","fan","ant","horse","mode"]
L.sort(key=lambda a: len(a))
L = L[::-1] # <-- had to do this way as otherwise fan comes before ant if you do reverse=True in the sort function.
print(L)
+ 3
When you say "most alphabets", do you mean most total letters or most unique letters. E.g. Mississippi has 11 total letters but only 4 unique letters.
+ 2
lambda x: len(set(x))
+ 1
print(sorted(L, key=len, reverse=True))
0
David Ashton, I mean "most total letters"but I would also appreciate the solution for most unique letters. Thanks