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
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = list (test_str.split())
print ( "The list of stream of numbers : " + str (res))
|
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.
Python3
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = list ( map ( int , test_str.split()))
print ( "The list of stream of numbers : " + str (res))
|
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 #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
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = re.findall(r '\d+' , test_str)
res = list ( map ( int , res))
print ( "The list of stream of numbers : " + str (res))
|
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]
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
test_str = "10 12 3 54 6 777 443"
res = [ int (num) for num in test_str.split()]
print ( "The original string is : " + test_str)
print ( "The list of stream of numbers:" , res)
|
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]
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
test_str = "10 12 3 54 6 777 443"
res = []
for idx, num in enumerate (test_str.split()):
res.append( int (num))
print ( "The original string is : " + test_str)
print ( "The list of stream of numbers:" , res)
|
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]
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 straightforw
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 u
2 min read
Create List of Numbers with Given Range - Python
The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8
3 min read
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 the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss
6 min read
Python Program to Convert a List to String
In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it. Using th
3 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd
6 min read
Python | Convert String to tuple list
Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + split() + replace() This is a br
5 min read