-3

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:

enter image description here

This command returns a of type <map at 0x1f3759638c8>. How to iterate or access this a?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • By the way you can still iterate it just the same. i.e. doing `for num in a:`. If you want a view of all the items, just convert to a `list`... – Tomerikoo Feb 15 '21 at 16:54
  • See also: [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user) – Tomerikoo Feb 15 '21 at 16:55
  • Lastly, why are you trying to convert words to integers? That will raise an error later – Tomerikoo Feb 15 '21 at 16:56
  • 1
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Include your code as a [formatted code block](//stackoverflow.com/help/formatting) instead of an image. Please also take the [tour], read [what's on-topic](/help/on-topic) and [ask]. [Asking on Stack Overflow is not a substitute for doing your own research.](//meta.stackoverflow.com/a/261593/843953) Welcome to Stack Overflow! – Pranav Hosangadi Feb 15 '21 at 16:56

2 Answers2

0

Try simply doing the following -

a = list(map(int, input().split(' ')))
print(a)
> 5 4 3
[5, 4, 3]
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
0

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:-

  1. When you use input() , the input taken is in form of string.
  2. Using split(' ') splits the string on every occurrence of space and a list will be created.
    Value of that list will be ['Python','is','fun']
  3. Now what map function does is, it applies int() on every element of the list. So basically what you are trying to do is int('Python'), which will give an error.

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