Python | Convert Stream of numbers to list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert Stream of numbers to list
# Using list() + split()
  
# initializing string 
test_str = "10 12 3 54 6 777 443"
  
# printing original string 
print("The original string is : " + test_str)
  
# Using list() + split()
# Convert Stream of numbers to list
res = list(test_str.split())
  
# printing result 
print("The list of stream of numbers : " + str(res))

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert Stream of numbers to list
# Using map() + split() + list()
  
# initializing string 
test_str = "10 12 3 54 6 777 443"
  
# printing original string 
print("The original string is : " + test_str)
  
# Using map() + split() + list()
# Convert Stream of numbers to list
res = list(map(int, test_str.split()))
  
# printing result 
print("The list of stream of numbers : " + str(res))

chevron_right


Output :

The original string is : 10 12 3 54 6 777 443
The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.