A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-converting-all-strings-in-list-to-integers/ below:

Converting all Strings in a List to Integers - Python

Converting all Strings in a List to Integers - Python

Last Updated : 11 Jul, 2025

We are given a list of strings containing numbers and our task is to convert these strings into integers. For example, if the input list is ["1", "2", "3"] the output should be [1, 2, 3]. Note: If our list contains elements that cannot be converted into integers such as alphabetic characters, strings or special symbols, trying to convert them using int() will raise a ValueError or Invalid Literal Error. Let's discuss various ways to convert all string elements in a list to integers in Python.

Using map()

map() function applies a given function to all elements in an iterable. Here, we use map(int, a) to convert each string in the list to an integer.

Python
a = ['2', '4', '6', '8']

b = list(map(int, a))
print(b)

Explanation:

Using list comprehension

List comprehension provides a more Pythonic and concise way to convert a list of strings to integers as it combines the conversion and iteration into a single line of code.

Python
a = ['2', '4', '6', '8']

b = [int(item) for item in a]
print(b)

Explanation:

Using a loop

In this approach we iterate over the list using a loop (for loop) and convert each string to an integer using the int() function.

Python
a = ['2', '4', '6', '8']

for i in range(len(a)):
  
    a[i] = int(a[i])

print(a)

Explanation:

Related Articles:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4