python filter() function | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

python filter() function

# how can i do this with the python filter() function # i have spent way to much time on this already.. # i don't understand how to use filter() to Iterate a list?? names = ["David", "John", "Annabelle", "Johnathan", "Veronica"] result = [] for word in names: if len(word) >5: result.append(word) print(result)

29th Apr 2021, 9:50 PM
userx
userx - avatar
3 Réponses
+ 4
res = list(filter(lambda x: len(x) > 5, names)) print(res)
29th Apr 2021, 9:55 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg: omg thank you i knew it was something like that but for the life of me i couldn't figure it out i tried len() every way i could think of
29th Apr 2021, 9:59 PM
userx
userx - avatar
0
If you can convert the condition len(word) > 5 into a function, then you would be half done.
29th Apr 2021, 9:54 PM
Seb TheS
Seb TheS - avatar