Why is the result of this code not a list, or is my code wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why is the result of this code not a list, or is my code wrong?

a = [1,2,3] b = map(lambda x: x**2, a) print (b)

23rd Dec 2019, 8:38 PM
Charles Xstorm Ukadike
Charles Xstorm Ukadike - avatar
3 Answers
+ 4
The map function is in fact an object. So b isn't a list but an iterable map object... That can be converted into a list.
23rd Dec 2019, 9:09 PM
Théophile
Théophile - avatar
+ 3
Okay, so i converted the end result using the "convert to list" function and it worked. I wrote print(list(b)) instead of print(b). I'm not sure why it doesn't give a straight list result. :(
23rd Dec 2019, 8:43 PM
Charles Xstorm Ukadike
Charles Xstorm Ukadike - avatar
+ 3
just to add (if it helps) ....as the map returns an iterable object, you can do this: a = [1, 2, 3] for a in map(lambda x: x**2, a): print(a)
23rd Dec 2019, 9:49 PM
rodwynnejones
rodwynnejones - avatar