Lambda | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Lambda

I want to see an example with function lambda with list . Maybe it have a map - function.

15th Sep 2020, 6:50 PM
Petr
16 Answers
+ 2
Petr are you talking about this ? print(list(map(lambda x:x if x%21==1 else "",(i for i in range(1,100))))) If yes, it's not an appropriate choice if you only want the element that satisfies the condition ,filter function can help you in that case print(list(filter(lambda x:x%21==1,(i for i in range(1,100)))))
15th Sep 2020, 7:31 PM
Abhay
Abhay - avatar
+ 5
Petr although map is supported by python, it is not pythonic. Abhay gave a perfect answer but pythonic way for eg first letter of word [x[0] for x in ["spam", "egg"]]
15th Sep 2020, 7:18 PM
Oma Falk
Oma Falk - avatar
+ 3
Abhay result must be spcm, and your result is ["s""p""c""m"]. But your example is perfect for me thanks!
15th Sep 2020, 7:13 PM
Petr
+ 3
Oma Falk. :)) i want understand lambda and map. It is difficult for me in case, when i use more then two variables
15th Sep 2020, 7:20 PM
Petr
+ 3
Petr right.. it will help if you learn further languages! especially functional programming.
15th Sep 2020, 7:23 PM
Oma Falk
Oma Falk - avatar
+ 3
Which Lambda are you referring to? Is it the one related with Physics calculations?
17th Sep 2020, 5:55 PM
Oladimeji Abdrahman
Oladimeji Abdrahman - avatar
+ 2
For example, I have a list of strings and i want in one line write function for search the max string of it with repalce one char to other ("a" replace "o")
15th Sep 2020, 6:52 PM
Petr
+ 2
Something like this ? a=["foo","spam"] print(list(map(lambda x:"c" if x=="a" else x,max(a))))
15th Sep 2020, 7:10 PM
Abhay
Abhay - avatar
+ 2
Abhay and if i want get list of range for only elements%21=1?
15th Sep 2020, 7:15 PM
Petr
+ 2
Abhay you are the BEST!!! Thank you!
15th Sep 2020, 7:32 PM
Petr
+ 2
Mirielle[ InAcTiVe ] may be you have example of so combination map and iter-function?
15th Sep 2020, 7:41 PM
Petr
17th Sep 2020, 9:40 PM
Petr
0
You can use join method of string to get output as spcm print("".join(list(map(lambda x:"c" if x=="a" else x,max(a)))))
15th Sep 2020, 7:18 PM
Abhay
Abhay - avatar
0
Hello, you can use a lambda function in a Python list comprehension as follows: >>> [(lambda x:x*x)(x) for x in range(1,4)] [1, 4, 9] But there is no point in doing so when the comprehension essentially gives you the lambda for free (you simply write the expression that would have been in the body of the lambda: >>> [x*x for x in range(1,4)] [1, 4, 9] I hope this will help to you :)
24th Sep 2020, 4:45 AM
Ishan Shah
Ishan Shah - avatar
0
x = int(input()) y = (lambda z:z*z*z)(x) print(y)
25th Mar 2022, 4:01 PM
Sona Variyamveetle
0
y=(lambda z:z**3)(x)
22nd Nov 2022, 3:40 PM
Adike Sidhartha rao
Adike Sidhartha rao - avatar