Python | Convert list of numerical string to list of Integers

Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in form of string that too as a list in list and we need to segregate it to be digit separated integers. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using list comprehension
This problem can be dealt with list comprehension, as a shorthand to generic loops that we would require to run to perform this particular task by iterating each list’s each character of string and converting to integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# converting string list to integer list
# using list comprehension
  
# initializing list
test_list = [['24'], ['45'], ['78'], ['40']]
  
# printing original list
print("The original list : " + str(test_list))
  
# using list comprehension
# converting string list to integer list
res = [[int(i) for i in sub] for i in test_list for sub in i]
  
# print result
print("The list after conversion : " + str(res))

chevron_right


Output :

The original list : [['24'], ['45'], ['78'], ['40']]
The list after conversion : [[2, 4], [4, 5], [7, 8], [4, 0]]

 

Method #2 : Using map() + list comprehension
The task performed in the above function can also be reduced to include the map function which bind the conversion to integer to a function and rest of task is performed by the list comprehension function itself.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# converting string list to integer list
# using list comprehension + map()
  
# initializing list
test_list = [['24'], ['45'], ['78'], ['40']]
  
# printing original list
print("The original list : " + str(test_list))
  
# using list comprehension + map()
# converting string list to integer list
res = [list(map(int, list(sub[0]))) for sub in test_list if sub ]
  
# print result
print("The list after conversion : " + str(res))

chevron_right


Output :

The original list : [['24'], ['45'], ['78'], ['40']]
The list after conversion : [[2, 4], [4, 5], [7, 8], [4, 0]]


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.