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.



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

Original list is : ['1', '4', '3', '6', '7']
Modified list is : [1, 4, 3, 6, 7]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.