I am taking input from the user in single line string input. Trying to convert the input string into a list of numbers using the below-mentioned command:
This command returns a
of type <map at 0x1f3759638c8>
.
How to iterate or access this a
?
I am taking input from the user in single line string input. Trying to convert the input string into a list of numbers using the below-mentioned command:
This command returns a
of type <map at 0x1f3759638c8>
.
How to iterate or access this a
?
Try simply doing the following -
a = list(map(int, input().split(' ')))
print(a)
> 5 4 3
[5, 4, 3]
You will get an error for the input that you have given i.e. 'Python is fun' because map will try to map the input to function int(). Let me explain:-
To avoid this use the following code snippet:
a = map(int, input().split(' '))
'1 2 3 4'
for i in a:
print(i)
Output of the above code will be
1
2
3
4