Sometimes, we can be stuck with a problem in which we are given a stream of space separated numbers with a goal to convert them into a list of numbers. This type of problem can occur in common day-day programming or competitive programming while taking inputs. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using
list() + split()
The space separated numbers can be converted to list by using a simple
split function that would convert the string to list of numbers and hence solve our problem.
The original string is : 10 12 3 54 6 777 443 The list of stream of numbers : ['10', '12', '3', '54', '6', '777', '443']
Method #2 : Using
map() + split() + list()
Since the drawback of above method is that the conversion doesn’t change the datatype of the unit numbers, so if it’s desired to change the data type of number as well, it is suggested to additionally use
map() to have list of strings as integers.
The original string is : 10 12 3 54 6 777 443 The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of strings
- Python | Convert 1D list to 2D list of variable length
- Python | Convert Integral list to tuple list
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Convert a nested list into a flat list
- Python | Convert list of strings to list of tuples
- Python | Convert list to indexed tuple list
- Python | Convert list into list of lists
- Python | Convert list of tuples into list
- Python | Ways to Convert a 3D list into a 2D list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.