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?
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?
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)
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]
>>>