Convert all strings in a list to integers [duplicate]

Use map then list to obtain a list of integers:

list(map(int, xs)) 

In Python 2, list was unnecessary since map returned a list:

map(int, xs) 
3,338 11 11 gold badges 59 59 silver badges 115 115 bronze badges answered Sep 10, 2011 at 0:30 34.5k 4 4 gold badges 36 36 silver badges 43 43 bronze badges

I want to point out that pylint discourages the use of map , so prepare to use list comprehensions anyway if you ever use that standard. :)

Commented Feb 12, 2015 at 6:41 The inverse is ( to convert a list of int to list of string ) : map( str, results) Commented Mar 11, 2017 at 22:08

You can simplify this answer: just always use list(map(int, results)) , it works for any Python version.

Commented Jan 24, 2018 at 18:50

I just want to add for the map function its type is an iterable so technically if you were to iterate through it you wouldn't need to convert it into a list.

Commented Dec 31, 2020 at 16:38

Use a list comprehension on the list xs :

[int(x) for x in xs] 
>>> xs = ["1", "2", "3"] >>> [int(x) for x in xs] [1, 2, 3] 
26.7k 21 21 gold badges 115 115 silver badges 149 149 bronze badges answered Sep 10, 2011 at 0:52 8,732 2 2 gold badges 29 29 silver badges 36 36 bronze badges

List comprehension is great too. To the OP - see here for to see a nice contrast of map and list comprehension: stackoverflow.com/questions/1247486/…

Commented Sep 10, 2011 at 2:08

There are several methods to convert string numbers in a list to integers.

In Python 2.x you can use the map function:

>>> results = ['1', '2', '3'] >>> results = map(int, results) >>> results [1, 2, 3] 

Here, It returns the list of elements after applying the function.

In Python 3.x you can use the same map

>>> results = ['1', '2', '3'] >>> results = list(map(int, results)) >>> results [1, 2, 3] 

Unlike python 2.x, Here map function will return map object i.e. iterator which will yield the result(values) one by one that's the reason further we need to add a function named as list which will be applied to all the iterable items.

Refer to the image below for the return value of the map function and it's type in the case of python 3.x

map function iterator object and it

The third method which is common for both python 2.x and python 3.x i.e List Comprehensions

>>> results = ['1', '2', '3'] >>> results = [int(i) for i in results] >>> results [1, 2, 3] 
answered Feb 12, 2022 at 21:02 Shubhank Gupta Shubhank Gupta 795 2 2 gold badges 12 12 silver badges 33 33 bronze badges Nice to know what works in 2 in 3 and in both. Commented May 12 at 18:32

You can easily convert string list items into int items using loop shorthand in python

Say you have a string result = ['1','2','3']

result = [int(item) for item in result] print(result) 

It'll give you output like

69.9k 36 36 gold badges 188 188 silver badges 256 256 bronze badges answered Jun 23, 2021 at 11:19 danialcodes danialcodes 185 2 2 silver badges 1 1 bronze badge The same solution has already been posted above. Commented Jun 23, 2021 at 11:57

If your list contains pure integer strings, the accepted answer is the way to go. It will crash if you give it things that are not integers.

So: if you have data that may contain ints, possibly floats or other things as well - you can leverage your own function with errorhandling:

def maybeMakeNumber(s): """Returns a string 's' into a integer if possible, a float if needed or returns it as is.""" # handle None, "", 0 if not s: return s try: f = float(s) i = int(f) return i if f == i else f except ValueError: return s data = ["unkind", "data", "42", 98, "47.11", "of mixed", "types"] converted = list(map(maybeMakeNumber, data)) print(converted) 
['unkind', 'data', 42, 98, 47.11, 'of mixed', 'types'] 

To also handle iterables inside iterables you can use this helper:

from collections.abc import Iterable, Mapping def convertEr(iterab): """Tries to convert an iterable to list of floats, ints or the original thing from the iterable. Converts any iterable (tuple,set, . ) to itself in output. Does not work for Mappings - you would need to check abc.Mapping and handle things like when converting them - so they come out as is.""" if isinstance(iterab, str): return maybeMakeNumber(iterab) if isinstance(iterab, Mapping): return iterab if isinstance(iterab, Iterable): return iterab.__class__(convertEr(p) for p in iterab) data = ["unkind", , "data", "42", 98, "47.11", "of mixed", ("0", "8", , "3.141"), "types"] converted = convertEr(data) print(converted) 
['unkind', , 'data', 42, 98, 47.11, 'of mixed', (0, 8, , 3.141), 'types'] # sets are unordered, hence diffrent order