Sergei
Sergei

Reputation: 605

Trouble with map()

I'm trying to convert the values of a list using the map function but i am getting a strange 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 does it not return ['4','58','6']?

Upvotes: 5

Views: 165

Answers (2)

mshsayem
mshsayem

Reputation: 18008

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

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

Upvotes: 2

jamylak
jamylak

Reputation: 133504

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

Call list(x) on the variable after you assign it the map generator.

Upvotes: 10

Related Questions