Convert string to integer using map()

>>> T1 = ['13', '17', '18', '21', '32']
>>> print [int(x) for x in T1]
[13, 17, 18, 21, 32]

You don't need map inside your list comprehension. Map creates another list so you end up with a list of list.

Caveat: This will work if the strings are granted to be numbers otherwise it will raise exception.


>>> T1 = ['13', '17', '18', '21', '32']
>>> T3 = list(map(int, T1))
>>> T3
[13, 17, 18, 21, 32]

This does the same thing as:

>>> T3 = [int(x) for x in T1]
>>> T3
[13, 17, 18, 21, 32]

so what you are doing is

>>> T3 = [[int(letter) for letter in x] for x in T1]
>>> T3
[[1, 3], [1, 7], [1, 8], [2, 1], [3, 2]]

Hope that clears up the confusion.


You can do it like this

>>>T1 = ['13', '17', '18', '21', '32']
>>>list(map(int,T1))

Tags:

Python