0

Can't understand why I can't assign map result to a variable. It's losing its value.

>>> L = 'something'
>>> R = map(lambda x: x * 2, L)
>>> print(list(R))
['ss', 'oo', 'mm', 'ee', 'tt', 'hh', 'ii', 'nn', 'gg']
>>> V = list(R)
>>> print(V)
[]
Exa
  • 4,020
  • 7
  • 43
  • 60
John Doe
  • 57
  • 8

2 Answers2

0

Because the map object can be iterated over only once. When you print it, that's it. The next time you assign it, it's empty

Instead, try

R = map(..)
V = list(R)
print(V)
blue_note
  • 27,712
  • 9
  • 72
  • 90
0

In Python 3 map returns an iterator that, once consumed, will yield nothing. In your case you are consuming it in the first print(list(R)) call, so in the second one it yields nothing which is the same as list() alone.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Netwave
  • 40,134
  • 6
  • 50
  • 93