You may apply either of the two methods below in order to convert strings to integers in a Python list:

**(1) Using map**

mylist = ['value1','value2','value3',...] mylist = list(map(int, mylist))

**(2) Using list comprehension**

mylist = ['value1','value2','value3',...] mylist = [int(i) for i in mylist]

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 *string* (by placing each value within quotes):

mylist = ['22','45','75','68','39'] print (mylist)

Here is how the list with the strings would look like:

You can then use **map** in order to convert the strings to integers:

mylist = ['22','45','75','68','39'] mylist = list(map(int, mylist)) print (mylist)

Once you run the code, you’ll see the list of integers (without any quotes around the values):

Optionally, you may perform a further check to verify that the strings got converted to integers:

mylist = ['22','45','75','68','39'] print (mylist) for i in mylist: check = type(i) print (i,check) mylist = list(map(int, mylist)) print (mylist) for i in mylist: check = type(i) print (i,check)

The portion in red represents the strings *prior* to the conversion, while the portion in green represents the integers following the conversion:

## Using List Comprehension to Convert Strings to Integers in a Python List

Alternatively, you may use the second method to convert your strings to integers:

mylist = ['value1','value2','value3',...] mylist = [int(i) for i in mylist]

For our example:

mylist = ['22','45','75','68','39'] mylist = [int(i) for i in mylist] print (mylist)

The strings will be converted to integers:

As before, you may apply the syntax below in order to verify that the strings got converted to integers:

mylist = ['22','45','75','68','39'] print (mylist) for i in mylist: check = type(i) print (i,check) mylist = [int(i) for i in mylist] print (mylist) for i in mylist: check = type(i) print (i,check)

You’ll get the same results as under the first method:

## Use Case: Why Would you Want to Convert Strings to Integers?

Let’s now review a simple use case, where you have a list with strings. The goal is to deduct 5 from each value within the list:

mylist = ['22','45','75','68','39'] for i in mylist: i = i-5 print (i)

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:

mylist = ['22','45','75','68','39'] mylist = list(map(int, mylist)) for i in mylist: i = i-5 print (i)

Once the strings got converted to integers, you’ll be able to successfully deduct 5 from each value within the list:

Using a list comprehension:

mylist = ['22','45','75','68','39'] mylist = [int(i)-5 for i in mylist] print (mylist)

And the result: