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"]

6th Sep 2019, 10:31 PM
Fabian Nwobi
Fabian Nwobi - avatar
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)
6th Sep 2019, 10:56 PM
rodwynnejones
rodwynnejones - avatar
+ 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.
7th Sep 2019, 5:16 AM
David Ashton
David Ashton - avatar
+ 2
lambda x: len(set(x))
7th Sep 2019, 7:59 AM
HonFu
HonFu - avatar
+ 1
print(sorted(L, key=len, reverse=True))
6th Sep 2019, 10:36 PM
HonFu
HonFu - avatar
0
David Ashton, I mean "most total letters"but I would also appreciate the solution for most unique letters. Thanks
7th Sep 2019, 7:06 AM
Fabian Nwobi
Fabian Nwobi - avatar