You may apply either of the two approaches below in order to convert strings to integers in a Python list:
(1) Using map
my_list = ['value1','value2','value3',...] my_list = list(map(int, my_list))
(2) Using list comprehension
my_list = ['value1','value2','value3',...] my_list = [int(i) for i in my_list]
Let’s now review several examples.
Using Map to Convert Strings to Integers in a Python List
Suppose that you have a list that contains 5 values, where each value is stored as a string (by placing each value within quotes):
my_list = ['22','45','75','68','39'] print (my_list)
Here is how the list with the strings would look like:
['22', '45', '75', '68', '39']
You can then use map in order to convert the strings to integers:
my_list = ['22','45','75','68','39'] my_list = list(map(int, my_list)) print (my_list)
Once you run the code, you’ll see the list of integers (without any quotes around the values):
[22, 45, 75, 68, 39]
Optionally, you may perform a further check to verify that the strings were converted to integers:
my_list = ['22','45','75','68','39'] print (my_list) for i in my_list: check = type(i) print (i,check) my_list = list(map(int, my_list)) print (my_list) for i in my_list: check = type(i) print (i,check)
The portion in yellow represents the strings prior to the conversion, while the portion in green represents the integers following the conversion:
['22', '45', '75', '68', '39']
22 <class 'str'>
45 <class 'str'>
75 <class 'str'>
68 <class 'str'>
39 <class 'str'>
[22, 45, 75, 68, 39]
22 <class 'int'>
45 <class 'int'>
75 <class 'int'>
68 <class 'int'>
39 <class 'int'>
Using List Comprehension to Convert Strings to Integers in a Python List
Alternatively, you may use the second approach to convert your strings to integers:
my_list = ['value1','value2','value3',...] my_list = [int(i) for i in my_list]
For our example:
my_list = ['22','45','75','68','39'] my_list = [int(i) for i in my_list] print (my_list)
The strings will be converted to integers:
[22, 45, 75, 68, 39]
As before, you may apply the syntax below in order to verify that the strings were converted to integers:
my_list = ['22','45','75','68','39'] print (my_list) for i in my_list: check = type(i) print (i,check) my_list = [int(i) for i in my_list] print (my_list) for i in my_list: check = type(i) print (i,check)
You’ll get the same results as the first approach:
['22', '45', '75', '68', '39']
22 <class 'str'>
45 <class 'str'>
75 <class 'str'>
68 <class 'str'>
39 <class 'str'>
[22, 45, 75, 68, 39]
22 <class 'int'>
45 <class 'int'>
75 <class 'int'>
68 <class 'int'>
39 <class 'int'>
Use Case: Why Convert Strings to Integers?
Let’s now review a simple use case, where we’ll have a list with strings. The goal is to deduct 5 from each value within the list:
my_list = ['22','45','75','68','39'] for i in my_list: i = i-5
If you try to apply the above code, you’ll get the following error:
TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’
One way to overcome this error is by converting the strings to integers, so that you can perform various arithmetic calculations:
my_list = ['22','45','75','68','39'] my_list = list(map(int, my_list)) new_list = [] for i in my_list: i = i-5 new_list.append(i) print (new_list)
Once the strings got converted to integers, you’ll be able to successfully deduct 5 from each value within the list:
[17, 40, 70, 63, 34]
Using a list comprehension:
my_list = ['22','45','75','68','39'] new_list = [int(i)-5 for i in my_list] print (new_list)
And here is the result:
[17, 40, 70, 63, 34]