Convert List from Character String to Integer in Python (2 Examples)
In this article you’ll learn how to transform values in a list from string to integer in the Python programming language.
The article contains these contents:
Let’s dive right in:
Creating Example Data
At the start, we’ll need to define some example data:
my_list = ['5', '3', 7, '2', 5, '4'] # Create example list print(my_list) # Print example list # ['5', '3', 7, '2', 5, '4']
As you can see based on the previously shown output of the Python console, our example data is a list object that contains six elements.
Let’s check the data type of each item in our list:
for i in my_list: print(type(i)) # Return data types of all list elements # <class 'str'> # <class 'str'> # <class 'int'> # <class 'str'> # <class 'int'> # <class 'str'>
As you can see, four of the six list elements have the string data type.
Let’s modify our list so that all list items have the integer data type!
Example 1: Transform List Elements from String to Integer Using map() Function
In Example 1, I’ll illustrate how to employ the map function to change the data type of character strings in a list to integer.
Have a look at the following Python syntax and its output:
my_list_int1 = list(map(int, my_list)) # Apply map function print(my_list_int1) # Print updated list # [5, 3, 7, 2, 5, 4]
You might already notice a difference between the previous output and the original list that we have created at the beginning of the tutorial: The quotation marks disappeared!
Let’s print the data type of all the elements in our list using the type function:
for i in my_list_int1: print(type(i)) # Return data types of all list elements # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'>
Only integers, great!
Example 2: Transform List Elements from String to Integer Using List Comprehension
Alternatively to the map function shown in Example 1, we may also use list comprehension to convert character strings to integer.
Example 2 explains how to achieve that based on our example list. Consider the Python code below:
my_list_int2 = [int(i) for i in my_list] # Apply list comprehension print(my_list_int2) # Print updated list # [5, 3, 7, 2, 5, 4]
As you can see, the previous output looks exactly the same as the output of Example 1.
However, let’s double-check this using the type function once again:
for i in my_list_int2: print(type(i)) # Return data types of all list elements # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'>
Again only integers! This time we have used list comprehension instead of the map function, though.
Video, Further Resources & Summary
Do you need further info on the Python code of this article? Then you might have a look at the following video on my YouTube channel. In the video, I’m explaining the contents of this post.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at the related tutorials on this website. You can find some tutorials about topics such as character strings and data conversion below:
- Count Duplicates in List in Python
- How to Use Lists in Python
- Convert Integer to String in pandas DataFrame Column in Python
- Convert String to Integer in pandas DataFrame Column in Python
- Python Programming Overview
Summary: You have learned in this article how to convert elements in a list object from string to integer in Python programming. In case you have any further questions, let me know in the comments below.
Statistics Globe Newsletter