1

I´m running the following code:

selectedvalidations='1,2,3,4,5,6,7'
x=map(int,selectedvalidations.split(','))
print(list(x))

It prints:

[1, 2, 3, 4, 5, 6, 7]

If I print again:

print(list(x))

It prints:

[]

Why?

Mauro Assis
  • 375
  • 1
  • 5
  • 22

2 Answers2

1

In python3 map is a generator. If you want to reuse the variable use

selectedvalidations='1,2,3,4,5,6,7'
x=list(map(int,selectedvalidations.split(',')))  #Encapsulate map in list
print(x)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

The right way to do it is to convert the generator into list in-place and hold the list in the variable

>>> selectedvalidations='1,2,3,4,5,6,7'
>>> x=list(map(int,selectedvalidations.split(',')))
>>> x
[1, 2, 3, 4, 5, 6, 7]
>>> x
[1, 2, 3, 4, 5, 6, 7]
>>> 
Sijan Bhandari
  • 2,941
  • 3
  • 23
  • 36