How to Convert List of Strings to Ints in python : 4 Methods

How to Convert List of Strings to Ints in python

Do you want to Convert List of Strings to Ints in python? If yes then you have come to the right place. In this entire post, you will know the various methods to convert the list of strings to int in python.

Methods to Convert List of Strings to Ints in python

Before going to the methods let’s create a sample list of strings. The list contains all the numerical values, but of string type. Let’s create them.

string_list = ["10","20","30","40","50","60"]
print(string_list)

Output

Sample list of strings
Sample list of strings

You can see in the above figure all the numerical values are in a single quote. It clearly represents that all these are strings.

Method 1:  Convert String Array to Int array using python map

In this method, I will use the Python map() function to achieve the results. Just pass your string array and typecast the results to the list(). And you will get the list of int.

Execute the following code.

str_to_int = list(map(int,string_list))
print(str_to_int)

Output

Convert String Array to Int array using python map
Convert String Array to Int array using python map

Method 2: Convert List of strings to ints using list comprehension in python

The second method to convert string to int is using the list comprehension in python. Inside the list comprehension, there will be a loop that converts a string to int.

Execute the following code.

str_to_int2 = [int(x) for x in string_list]
print(str_to_int2)

Output

Convert List of strings to int using list comprehension
Convert List of strings to int using list comprehension

Method 3: Convert List of strings to ints using a new list

You can also convert list of strings to ints by using the new or fresh list. After that, you have to append all the converted strings to that list. And if you print the new list, you will get all the values as int.

string_list = ["10","20","30","40","50","60"]
int_list = []
for v in string_list:
    int_list.append(int(v))

print(int_list)

You can see in the above code in each iteration of the loop, the value is typecasting to int and appending to the int_list.

Output

Convert List of strings to ints using new list
Convert List of strings to ints using new list

Method 4: Convert strings to ints in python using NumPy array

In this method, I will use the NumPy python module for conversion. First I will convert the strings list to NumPy array. And then change the type of the array to int using the astype(“int”). Lastly, convert it again to the list.

Execute the following lines of code.

import numpy as np
string_list = ["10","20","30","40","50","60"]
result= np.array(string_list).astype("int")
print(list(result))

Output

Convert List of strings to ints using numpy module
Convert List of strings to ints using numpy module

Conclusion

These are the methods to Convert List of Strings to Ints in python. All methods are great and it depends on your usage. Hope We have solved your queries you are searching for. Even if you have any queries then you can contact us for more information. We are always ready to help you.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner