list_of_input = map(float,[1,2,3])
print(list(list_of_input)[0])
print(list(list_of_input)[0])
IndexError: list index out of range
Why does this error occur?
list_of_input = map(float,[1,2,3])
print(list(list_of_input)[0])
print(list(list_of_input)[0])
IndexError: list index out of range
Why does this error occur?
The conversion of a map to list is done only once, meaning map is a generator object and once you transform it to list, it gets exhausted : python 3: generator for map. So the error comes from second print statement and not the first.
in Python 3 map()
returns an iterator not a list
. When you pass this iterator to list
the first time it consumes the iterator so the second time you get an empty list
, hence the IndexError.