Skip to content

String list to int list Python | Example code

  • by

Use the map function to convert a String list to an int list in Python. To get a list of int you need to convert the result from a map to a list. This is the most elegant, pythonic, and recommended method to do this conversion.

Example string list to int list Python

Simple example code.

Using map()

test_list = ['1', '2', '3', '4', '5']

res = list(map(int, test_list))

print(res)
print(type(res))

Output:

String list to int list Python

Using list comprehension

To convert a list of strings to a list of integers in Python, you can use a list comprehension along with the int() function

str_list = ['1', '4', '3', '6', '7'] 

int_list = [int(i) for i in str_list]

print(int_list)

Output: [1, 4, 3, 6, 7]

Using for loop

Using a naive method to perform the conversion.

str_list = ['1', '4', '3', '6', '7']


for i in range(0, len(str_list)):
    str_list[i] = int(str_list[i])

print(str_list)

Output: [1, 4, 3, 6, 7]

Comment if you have any doubts or suggestions on this Python string list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *