Python | Convert Stream of numbers to list
Last Updated :
28 Feb, 2023
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.
Python3
# 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))
OutputThe 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.
Python3
# 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))
OutputThe original string is : 10 12 3 54 6 777 443
The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]
Method #3: Using re.findall() function
Using re.findall() function, we can find all digits in the string and then convert them to a list of integers
Python3
import re
# Initializing the string containing the numbers
test_str = "10 12 3 54 6 777 443"
# Printing the original string
print("The original string is : " + test_str)
# Using re.findall() function to find all digits in the string
res = re.findall(r'\d+', test_str)
# Using map() function to convert the digits to integers
res = list(map(int, res))
# Printing the result
print("The list of stream of numbers : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original string is : 10 12 3 54 6 777 443
The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]
The re.findall() function is from the re module, which is the python built-in regular expression module. The findall() function returns a list of all non-overlapping matches of the specified regular expression in the string. Here we are using regular expression \d+ which will match one or more digits. We are using findall() to find all occurrences of digits in the string. Then we are using map() function to convert the digits to integers. Finally, we are converting the map object to a list.
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using List comprehension:
Python3
# Initialize the stream of numbers as a string
test_str = "10 12 3 54 6 777 443"
# Use a list comprehension to convert the string to a list of integers
res = [int(num) for num in test_str.split()]
# Printing the original string
print("The original string is : " + test_str)
# Print the result
print("The list of stream of numbers:", res)
#This code is contributed by Jyothi pinjala.
OutputThe original string is : 10 12 3 54 6 777 443
The list of stream of numbers: [10, 12, 3, 54, 6, 777, 443]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5:Using enumeration
Algorithm:
- Initialize the stream of numbers as a string
- Split the string by space to get a list of numbers as strings
- Create an empty list to store the result
- Loop through each number string in the list
a. Convert the number string to integer using the int() function
b. Append the integer to the result list - Print the original string and the result list
Python3
#Initialize the stream of numbers as a string
test_str = "10 12 3 54 6 777 443"
#Use a list comprehension to convert the string to a list of integers
res = []
for idx, num in enumerate(test_str.split()):
res.append(int(num))
#Printing the original string
print("The original string is : " + test_str)
#Print the result
print("The list of stream of numbers:", res)
#This code is contributed by Vinay Pinjala.
OutputThe original string is : 10 12 3 54 6 777 443
The list of stream of numbers: [10, 12, 3, 54, 6, 777, 443]
Time complexity: O(n), where n is the number of numbers in the input string. This is because we need to loop through each number string in the list and convert it to an integer.
Auxiliary Space: O(n), where n is the number of numbers in the input string. This is because we need to store the list of numbers as integers in memory.
Similar Reads
Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa
3 min read
Python | Convert Joint Float string to Numbers Sometimes, while working with Legacy languages, we can have certain problems. One such can be working with FORTRAN which can give text output (without spaces, which are required) '12.4567.23' . In this, there are actually two floating point separate numbers but concatenated. We can have problem in w
5 min read
Python - Get summation of numbers in string list Sometimes, while working with data, we can have a problem in which we receive series of lists with data in string format, which we wish to accumulate as list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute force method to perform this
3 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
Python - Extract numbers from list of strings We are given a list of string we need to extract numbers from the list of string. For example we are given a list of strings s = ['apple 123', 'banana 456', 'cherry 789'] we need to extract numbers from the string in list so that output becomes [123, 456, 789].Using Regular ExpressionsThis method us
2 min read