Python | Converting all strings in list to integers
Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the entire list of string to integers is quite common in development domain. Let’s discuss few ways to solve this particular problem.
Method #1 : Naive Method
This is most generic method that strikes any programmer while performing this kind of operation. Just looping over whole list and convert each of the string of list to int by type casting.
# Python3 code to demonstrate # converting list of strings to int # using naive method # initializing list test_list = ['1', '4', '3', '6', '7'] # Printing original list print ("Original list is : " + str(test_list)) # using naive method to # perform conversion for i in range(0, len(test_list)): test_list[i] = int(test_list[i]) # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['1', '4', '3', '6', '7'] Modified list is : [1, 4, 3, 6, 7]
Method #2 : Using list comprehension
This is just a kind of replica of the above method, just implemented using list comprehension, kind of shorthand that a developer looks for always. It saves time and complexity of coding a solution.
# Python3 code to demonstrate # converting list of strings to int # using list comprehension # initializing list test_list = ['1', '4', '3', '6', '7'] # Printing original list print ("Original list is : " + str(test_list)) # using list comprehension to # perform conversion test_list = [int(i) for i in test_list] # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['1', '4', '3', '6', '7'] Modified list is : [1, 4, 3, 6, 7]
Method #3 : Using map()
This is the most elegant, pythonic and recommended method to perform this particular task. This function is exclusively made for this kind of task and should be used to perform them.
# Python3 code to demonstrate # converting list of strings to int # using map() # initializing list test_list = ['1', '4', '3', '6', '7'] # Printing original list print ("Original list is : " + str(test_list)) # using map() to # perform conversion test_list = list(map(int, test_list)) # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['1', '4', '3', '6', '7'] Modified list is : [1, 4, 3, 6, 7]
Recommended Posts:
- Python | Converting list string to dictionary
- Python | Convert list of numerical string to list of Integers
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Filter list of strings based on the substring list
- Python | Adding K to each element in a list of integers
- Python | Convert number to list of integers
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Program to print duplicates from a list of integers
- Python | Convert a list of multiple integers into a single integer
- Python | Reverse sequence of strictly increasing integers in a list
- Python | How to sort a list of strings
- Python | Remove all digits from a list of strings
- Python | Remove all strings from a list of tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




