Convert all strings in a list to int

Michael picture Michael · Sep 10, 2011 · Viewed 1.2M times · Source

In Python, I want to convert all strings in a list to integers.

So if I have:

results = ['1', '2', '3']

How do I make it:

results = [1, 2, 3]

Answer

cheeken picture cheeken · Sep 10, 2011

Use the map function (in Python 2.x):

results = map(int, results)

In Python 3, you will need to convert the result from map to a list:

results = list(map(int, results))