+ 3
Why this map return something?
8 ответов
+ 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.
+ 2
Kevin what is generator object? cam you explain
+ 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.
+ 2
Map function returns new iterables, so if you need to print its values then u should convert it to list.
+ 2
JVR27 thanks
+ 2
Mohamed Adel thanks
+ 1
thanks Kevin for explaining briefly👍
+ 1
list(map())