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 mixed data types tuple list to string list
- Python | Convert list of numerical string to list of Integers
- Python | Convert list of string into sorted list of integer
- Python Program to convert List of Integer to List of String
- Python | Convert string List to Nested Character List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of Dictionary to Tuple list
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Convert 1D list to 2D list of variable length
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert a nested list into a flat list
- Python | Convert List of lists to list of Strings
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.