Python | Convert list of tuples into digits
Last Updated :
31 Mar, 2023
Given a list of tuples, the task is to convert it into list of all digits which exists in elements of list. Let’s discuss certain ways in which this task is performed.
Method #1: Using re The most concise and readable way to convert list of tuple into list of all digits which exists in elements of list is by using re.
Python3
import re
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
temp = re.sub(r '[\[\]\(\), ]' , '', str (lst))
Output = [ int (i) for i in set (temp)]
print ( "Initial List is :" , lst)
print ( "Output list is :" , Output)
|
Output
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : [2, 1, 8, 4, 3, 0]
Method #2: Using itertools.chain() and lambda() This is yet another way to perform this particular task using lambda().
Python3
from itertools import chain
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
temp = map ( lambda x: str (x), chain.from_iterable(lst))
Output = set ()
for x in temp:
for elem in x:
Output.add(elem)
print ( "Initial List is :" , lst)
print ( "Output list is :" , Output)
|
Output
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : {'4', '3', '2', '1', '8', '0'}
Method #3: Using list(),map(),join() and set() methods
Initially we convert list containing tuple of integers to list containing tuple of strings.Later we will concatenate the string(obtained by joining tuple strings) to an empty string.And then use set to remove duplicates.Finally convert them to integer type and print output list
Python3
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
p = ""
for i in lst:
x = list ( map ( str ,i))
p + = "".join(x)
p = list ( map ( int , set (p)))
print ( "Initial List is :" , lst)
print ( "Output list is :" , p)
|
Output
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : [8, 3, 2, 4, 0, 1]
Time complexity: O(n), where n is the length of list
Auxiliary Space: O(m), where m is the number of digits.
Method #4: Using list comprehension and the isdigit method
You can use a list comprehension and the isdigit method to extract all the digits from the tuples in the list. The isdigit method returns True if a character is a digit, and False otherwise.
Python3
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
digits = set ()
print ( "The list of digits:" , digits)
|
Output
The list of digits: {'0', '8', '1', '2', '3', '4'}
Time complexity is O(n)
Auxiliary Space is O(m), where n is the length of the list and m is the number of digits.
Method #5: Using nested loops and string conversion
Step-by-step approach:
- Initialize the input list lst with the given values.
- Initialize an empty list digits to store the extracted digits.
- Loop through each tuple tup in the input list.
- Loop through each item in the current tuple.
- Loop through each character char in the string representation of the current item.
- Check if the current character is a digit using the isdigit() method.
- If it is a digit, convert it to an integer using the int() function and append it to the digits list.
- Use the set() function to remove duplicates from the digits list and convert it back to a list.
- Print the initial list and the output list.
Below is the implementation of the above approach:
Python3
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
digits = []
for tup in lst:
for item in tup:
for char in str (item):
if char.isdigit():
digits.append( int (char))
Output = list ( set (digits))
print ( "Initial List is :" , lst)
print ( "Output list is :" , Output)
|
Output
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : [0, 1, 2, 3, 4, 8]
Time complexity: O(n*m), where n is the length of the input list and m is the maximum length of the items in the tuples (assuming integer items only).
Auxiliary space: O(k), where k is the total number of digits in all the items in the input list.
Method #6: Using numpy.ravel():
- Import the numpy library.
- Initialize a list of tuples called last.
- Use numpy’s ravel() method to convert the list of tuples to a 1-D numpy array called arr.
- Use a list comprehension and the isdigit() method to extract the digits from the array and convert them to integers.
- Use set() to remove any duplicates in the list of digits.
- Convert the set back to a list called digits.
- Print the original list and the resulting list.
Python3
import numpy as np
lst = [( 11 , 100 ), ( 22 , 200 ), ( 33 , 300 ), ( 44 , 400 ), ( 88 , 800 )]
arr = np.array(lst).ravel()
digits = [ int (d) for d in ''.join( map ( str , arr)) if d.isdigit()]
digits = list ( set (digits))
print ( "Initial List is :" , lst)
print ( "Output list is :" , digits)
|
Output
Initial List is : [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
Output list is : [0, 1, 2, 3, 4, 8]
The time complexity of this code is O(n), where n is the total number of digits.
The space complexity of this code is O(n), where n is the total number of digits.
Share your thoughts in the comments
Please Login to comment...