Problem with map ()

I am trying to convert the values ​​of a list using a function map, but I get a weird result.

s = input("input some numbers: ")
i = map(int, s.split())
print(i)

gives:

input some numbers: 4 58 6
<map object at 0x00000000031AE7B8>

why doesn't it return ['4', '58', '6']?

+5
source share
2 answers

You are using python 3, which returns generators instead of lists.

Call list(x)in the variable after you have assigned the map generator to it.

+9
source

You need to do list(i)to get ['4','58','6']how mapin python 3 returns a generator instead of a list. Also, as Lattyware pointed out in the comment on the first answer, you'd better do this:

i = [int(x) for x in s.split()]
+2
source

All Articles