Why this map return something? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
23rd Sep 2018, 2:50 PM
Programmer Raja
Programmer Raja - avatar
8 Answers
+ 3
the map function performs a given function on every object of an iterable. if you need a visual return try f = tuple(map(lambda x: x*2,lie)) if you dont convert the map return into a collection, like a tuple, map returns a generator object, which means, you can only iterate once over it.
23rd Sep 2018, 3:02 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 2
Kevin what is generator object? cam you explain
23rd Sep 2018, 3:14 PM
Programmer Raja
Programmer Raja - avatar
+ 2
a generator object gives you the ability to iterate only once over it, like in a for-loop, and then the generator object vanishes. for example, try: a = map(lambda x: x*2, (1, 2, 3)) for i in a: print(i) for i in a: print(i) output: 1 4 6 as you can see, it iterated only once over the generator object. this method of list comprehension is a good practice if your code allows it. if you converted the map() return into a list, tuple or set, the output would be: 1 4 6 1 4 6 as you can see, converting the generator object into a collection gives you the ability to iterate as much as you want over it. google 'python generators' to learn more about generator functions with their yield return and generator objects.
23rd Sep 2018, 3:43 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
+ 2
Map function returns new iterables, so if you need to print its values then u should convert it to list.
25th Sep 2018, 9:04 AM
JVR27
JVR27 - avatar
+ 2
JVR27 thanks
25th Sep 2018, 1:00 PM
Programmer Raja
Programmer Raja - avatar
+ 2
Mohamed Adel thanks
8th Oct 2018, 4:08 PM
Programmer Raja
Programmer Raja - avatar
+ 1
thanks Kevin for explaining briefly👍
23rd Sep 2018, 3:59 PM
Programmer Raja
Programmer Raja - avatar
+ 1
list(map())
8th Oct 2018, 7:20 AM
Mohammed Adel
Mohammed Adel - avatar