-2

This is the code I edit on an editor and compile on a shell.
If I enter the integer 19, when I print out c, it is still ['1','9'] instead of the [1,9] I want. I tried this on the interactive interpreter instead of compiling a python file and it worked.

a = raw_input("Please enter a positive number ")    
c = []    
c = list(a)    
map(int, c) 
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user12551
  • 145
  • 1
  • 2
  • 9
  • Please [accept](http://meta.stackexchange.com/questions/5234) an answer if you think it solves your problem. It will community at large to recognize the correct solution. This can be done by clicking the green check mark next to the answer. See this [image](http://i.stack.imgur.com/uqJeW.png) for reference. Cheers. – Bhargav Rao Nov 29 '15 at 10:32

1 Answers1

5

You need to reassign the map output to c as it is not in-place

>>> a=raw_input("Please enter a positive number ")    
Please enter a positive number 19
>>> c = list(a) 
>>> c = map(int,c) # use the list() function if you are using Py3
>>> c
[1, 9]

See the docs on map

Apply function to every item of iterable and return a list of the results.

(emphasis mine)

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 2
    This will only run in Python 2.7.X. In Python 3.X you will have to `list(map(...))` because `map` in Python 3.X returns a map-object instead of a list directly. – daniel451 Jun 06 '15 at 23:41
  • 1
    @ascenator This program will not run in Py3 only because of `map` but also because of `raw_input`! I will edit the tags on the original question. Thanks – Bhargav Rao Jun 06 '15 at 23:42
  • 1
    Oh yes, right, I forgot about it. Of course input() is Python 3.X for raw_input() in Python 2.X and raw_input() therefore removed in 3.X. Tricky Python ;) – daniel451 Jun 06 '15 at 23:46
  • Yep. Anyway I'm up voting your comment to catch others attention :) – Bhargav Rao Jun 06 '15 at 23:51