Some weird thing in python code, map() function
Question:
arr = map(int, input().split())
for i in arr:
print(i)
print(list(arr)[1])
When I run this code and give the input as,
1 2 3 4 5
it gives the output as follows,
1
2
3
4
5
Traceback (most recent call last):
File "/home/muyustan/PycharmProjects/openCV/split.py", line 6, in <module>
print(list(arr)[1])
IndexError: list index out of range
Then,
arr = map(int, input().split())
# for i in arr:
# print(i)
print(list(arr)[1])
after commenting out the for loop and running the program and providing the same input, it gives the output at the terminal as follows;
2
Why using that for loop makes any changes?
Answers:
If you had printed arr
before the line of the exception, you’d have seen:
c:srvtmp> py3 weirdmap.py
1 2 3 4 5
<map object at 0x03611770>
2
the map-object is an iterator (that you can iterate over exactly once).
You could fix your code by either using a list comprehension:
arr = [int(v) for v in input().split()]
or by converting the iterator to a list before doing anything else with it:
arr = list(map(int, input().split()))
to see what is going on in the last line, consider this re-write:
mapobj = map(int, input().split())
arr = list(mapobj)
print(arr[2]) # <=== return the 3rd element
print(mapobj[2]) # <=== TypeError: 'map' object is not subscriptable
changing the last line to
print(list(mapobj)[2]) # <=== IndexError: list index out of range
since the arr = list(mapobj)
used up the iterator.
Map function doesn’t return a list but an iterator.
To obtain a list
arr = list(map(int, input().split()))
You can only loop once over an iterator.
When you write
arr = map(int, input().split())
No operations are made, you need to call the next() method over arr to obtain the next item. For loop does that.
arr = map(int, input().split())
for i in arr:
print(i)
print(list(arr)[1])
When I run this code and give the input as,
1 2 3 4 5
it gives the output as follows,
1
2
3
4
5
Traceback (most recent call last):
File "/home/muyustan/PycharmProjects/openCV/split.py", line 6, in <module>
print(list(arr)[1])
IndexError: list index out of range
Then,
arr = map(int, input().split())
# for i in arr:
# print(i)
print(list(arr)[1])
after commenting out the for loop and running the program and providing the same input, it gives the output at the terminal as follows;
2
Why using that for loop makes any changes?
If you had printed arr
before the line of the exception, you’d have seen:
c:srvtmp> py3 weirdmap.py
1 2 3 4 5
<map object at 0x03611770>
2
the map-object is an iterator (that you can iterate over exactly once).
You could fix your code by either using a list comprehension:
arr = [int(v) for v in input().split()]
or by converting the iterator to a list before doing anything else with it:
arr = list(map(int, input().split()))
to see what is going on in the last line, consider this re-write:
mapobj = map(int, input().split())
arr = list(mapobj)
print(arr[2]) # <=== return the 3rd element
print(mapobj[2]) # <=== TypeError: 'map' object is not subscriptable
changing the last line to
print(list(mapobj)[2]) # <=== IndexError: list index out of range
since the arr = list(mapobj)
used up the iterator.
Map function doesn’t return a list but an iterator.
To obtain a list
arr = list(map(int, input().split()))
You can only loop once over an iterator.
When you write
arr = map(int, input().split())
No operations are made, you need to call the next() method over arr to obtain the next item. For loop does that.