Convert List of Strings to Integer in Python

  1. Python Convert List of Strings to Integers Using the map() Function
  2. Python Convert List of String to Integers Using List Comprehension Method

This tutorial will explain various methods to convert a list of strings to a list of integers in Python. In many cases, we need to extract numerical data from strings and save it as an integer; for example, it can be a price of an item saved as a string or some identity number saved as a string.

Python Convert List of Strings to Integers Using the map() Function

The map(function, iterable) function applies function to each element of the iterable and returns an iterator.

To convert a list of strings to the list of integers, we will give int as the function to the map() function and a list of strings as the iterable object. Because the map() function in Python 3.x returns an iterator, we should use the list() function to convert it to the list.

string_list = ['100', '140', '8' ,'209', '50' '92', '3']
print(string_list)

int_list = list(map(int, string_list))
print(int_list)

Output:

['100', '140', '8', '209', '5092', '3']
[100, 140, 8, 209, 5092, 3]

Python Convert List of String to Integers Using List Comprehension Method

The other way we can use to convert the list of strings to the list of integers is to use list comprehension. The list comprehension creates a new list from the existing list. As we want to create a list of integers from a list of strings, the list comprehension method can be used for this purpose.

The code example below demonstrates how to use the list comprehension method to convert a list from string to integer.

string_list = ['100', '140', '8' ,'209', '50' '92', '3']
print(string_list)

int_list = [int(i) for i in string_list]
print(int_list)

Output:

['100', '140', '8', '209', '5092', '3']
[100, 140, 8, 209, 5092, 3]
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python List

  • Remove List Element by Index in Python
  • Initialize a 2D Array in Python
  • Related Article - Python String

  • Split String to Char Array in Python
  • Get Length of the List in Python
  • Related Article - Python Integer

  • Convert Bytes to String in Python 2 and Python 3
  • Generate Random Strings in Python