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 :/

15th May 2021, 6:40 AM
Da Silva Miguel
Da Silva Miguel - avatar
5 Antworten
+ 3
Da Silva Miguel Try this↓ names = ["David", "John", "Annabelle", "Johnathan", "Veronica"] print(list(filter(lambda x: len(x) >5,names)))
15th May 2021, 6:54 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 2
Use list comprehension like below. [x if len(x)>5 for x in names]
15th May 2021, 7:30 AM
Manikandan
Manikandan - avatar
+ 2
Manikandan the filter condition for list comprehension has to be at the end: print([x for x in names if len(x) > 5])
15th May 2021, 9:52 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
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... -_-
15th May 2021, 7:07 AM
Da Silva Miguel
Da Silva Miguel - avatar
0
My solution: result = list(filter(lambda x: len(x)>5, names)) print(result) Ask if u have questions!
17th Nov 2021, 5:54 PM
Stefan Bartl
Stefan Bartl - avatar