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.
# 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)) |
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.
# 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)) |
The original list : [['24'], ['45'], ['78'], ['40']] The list after conversion : [[2, 4], [4, 5], [7, 8], [4, 0]]
Recommended Posts:
- Python | Convert list of string to list of list
- Python | Convert list of string into sorted list of integer
- Python | Convert a string representation of list into list
- Python | Convert string enclosed list to list
- Python | Convert number to list of integers
- Python | Convert a list of multiple integers into a single integer
- Python | Convert list of tuples to list of list
- Python | Program to convert String to a List
- Python | Convert a list of characters into a string
- Python program to convert a list to string
- Python | Ways to convert list of ASCII value to string
- Python | Convert list of strings to space separated string
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert 1D list to 2D list of variable length
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.



