0
Python filtering exercise [SPOILER]
Hey everyone, Been struggling in the Python Filtering exercise for a while. I need to print only the names in the list who have 5+ letters. Already tried a while loop, creating a function() etc. When trying to use the filter() and len() together I only get the TypeError message. ### The list ### names = ["David", "John", "Annabelle", "Johnathan", "Veronica"] ############ My ideas run dried :/
5 odpowiedzi
+ 3
Da Silva Miguel
Try this↓
names = ["David", "John", "Annabelle", "Johnathan", "Veronica"]
print(list(filter(lambda x: len(x) >5,names)))
+ 2
Use list comprehension like below.
[x if len(x)>5 for x in names]
+ 2
Manikandan the filter condition for list comprehension has to be at the end:
print([x for x in names if len(x) > 5])
0
Thx for the help!
Used an identical code like yours but instead of writing lambda x: len(x)....
Only had written lambda x: x>5...
-_-
0
My solution:
result = list(filter(lambda x: len(x)>5, names))
print(result)
Ask if u have questions!