Hello, could anyone help me with this task | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello, could anyone help me with this task

I'm trying to do this test task If you are given a list of names, it should produce a list containing only names that consist of more than 5 characters. You can check the length of a string using the len () function and use the filter () function to define the condition. The thing is that I don't know how to use the len method in a lambda, and how to put the condition for the lambda start what the task wants. If anyone could help me with this I'll appreciate it.

27th Oct 2021, 3:31 AM
Alan Restrepo
Alan Restrepo - avatar
6 Answers
+ 1
A lambda is and acts like a function. It can accept arguments and return something. There's an example in the tutorial about how to use lambda as aggregate in a filter() function. May you be inspired from the example https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2461/
27th Oct 2021, 4:52 AM
Ipang
+ 1
Ipang thank you, you're right, the problem is that I don't know how to incorporate the function (len) in the lambda in order for to do what the problem asked
27th Oct 2021, 5:26 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
Have you gotten it figured out now though? I think you only need to make the lambda return a boolean (True/False) value depending on the list element's length. lambda ele: len( ele ) > 5 This lambda returns True when its argument's length was greater than 5
27th Oct 2021, 5:49 AM
Ipang
+ 1
Thank you so much Ipang , your code worked, Jesús Crist bless you, thanks for the help
27th Oct 2021, 7:51 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
lt = [x for x in str(input('Enter Names: ')).split()] print(list(filter(lambda i: len(i) > 5, lt)))
28th Oct 2021, 1:36 PM
Krishnam Raju M
Krishnam Raju M - avatar
+ 1
names = ["David", "John", "Annabelle", "Johnathan", "Veronica"] result = list(filter(lambda x:len(x)>5,names)) print(result)
1st May 2022, 6:41 AM
Vraj Soni
Vraj Soni - avatar