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
  • 303
  • 2
  • 17

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
  • 78,594
  • 17
  • 67
  • 103
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,801
  • 2
  • 17
  • 34