Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with map()

Tags:

python

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']?

like image 790
Sergei Avatar asked Apr 11 '12 13:04

Sergei


2 Answers

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

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

like image 65
jamylak Avatar answered Sep 20 '22 18:09

jamylak


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()]
like image 28
mshsayem Avatar answered Sep 21 '22 18:09

mshsayem