Ways to convert a list to integers in Python

Python is a powerful programming language that provides numerous ways to perform a variety of tasks. One common task that you may come across is converting a list of strings to a list of integers. In this article, we will explore all the ways to convert a list to integers in Python.

Using a For Loop

The most common way to convert a list of strings to a list of integers is to use a for loop. Here’s an example code snippet:

string_list = ['1', '2', '3', '4', '5']
int_list = []
for string in string_list:
    int_list.append(int(string))
print(int_list)
# Output : [1, 2, 3, 4, 5]

​In this code, we create an empty list called int_list to store the converted integers. Then, we use a for loop to iterate over each string in the original string list. We convert each string to an integer using the int() function and append it to the int_list.

Using List Comprehension

List comprehension is a powerful feature in Python that allows you to create a new list in a concise and readable way. Here’s how you can use list comprehension to convert a list of strings to a list of integers:

string_list = ['1', '2', '3', '4', '5']
int_list = [int(string) for string in string_list]
print(int_list)
​# Output : [1, 2, 3, 4, 5]

In this code, we use a list comprehension to create a new list of integers from the original string list. We convert each string to an integer using the int() function and store it in the int_list.

Using Map Function

The map() function is another way to convert a list of strings to a list of integers. Here’s how you can use the map() function to achieve this:

string_list = ['1', '2', '3', '4', '5']
int_list = list(map(int, string_list))
print(int_list)
# Output : [1, 2, 3, 4, 5]

In this code, we use the map() function to apply the int() function to each string in the string_list. The resulting map object is then converted to a list using the list() function and stored in the int_list.

Using NumPy Library

NumPy is a popular library for numerical computing in Python. It provides a function called numpy.asarray() that can be used to convert a list of strings to a list of integers. Here’s how you can use NumPy to achieve this:


import numpy as np
string_list = ['1', '2', '3', '4', '5']
int_list = np.asarray(string_list, dtype=int)
print(int_list)
# Output : [1 2 3 4 5]

In this code, we import the NumPy library using the import statement. Then, we use the numpy.asarray() function to convert the string_list to a NumPy array of integers.

Conclusion

In this article, we have explored all the ways to convert a list of strings to a list of integers in Python. You can choose the method that works best for your use case and programming style. Whether it’s using a for loop, list comprehension, map() function, or NumPy library, Python provides several options to make this task easy and efficient.

Fun Fact: You can check if a list empty or not in Python. Wanna know how? Learn More

Explore more from this category at Python Lists. Alternatively, search and view other topics at All Tutorials.