+ 5

Python Lambda List Range Question

i = list (range(1,4)) s = list (map(lambda x:x**2,i)) print(sum(s)) Solution: 14 Map lambda is asking me to repeat a function? #NOTE! I've since edited my post for errors. Thanks to all. ππ»

20th Mar 2019, 11:15 PM
tristach605
+ 8
2**2 == 4 3**2 == 9
21st Mar 2019, 12:05 AM
HonFu
+ 6
tristach605 list(range(1,4)) is a range from 1 to 3 inclusive, which means: "i = [1,2,3]"; "x**2" is "x" squared; print(sum(s)) => print(sum(1*1, 2*2, 3*3)) => print(1+4+9) => 14 βΊ
21st Mar 2019, 12:52 AM
Solo
+ 4
2**2= 2*2=4 2**3= 2*2*2=8
21st Mar 2019, 1:18 AM
π Prometheus πΈπ¬
+ 4
tristach605 Your lambda says x**2, not x**3 or x**x
21st Mar 2019, 6:58 AM
Anna
+ 2
Thanks! Any idea why range is only 1-3 (not 1-4)? Also, 2**2 is 4? 2x2x2 should be 8, shouldn't it? Sorry for the confusion and thanks for any help you could provide.
21st Mar 2019, 1:04 AM
tristach605
+ 2
You have written 3**3=27. But it should be 3**2=9. Also you have a syntax error at line 2. You have not closed the ()....
22nd Mar 2019, 1:49 AM