Open In App

Python | Convert list of tuples into digits

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






# Python code to convert list of tuples into
# list of all digits which exists
# in elements of list.
 
# Importing
import re
 
# Input list initialization
lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
 
# Using re
temp = re.sub(r'[\[\]\(\), ]', '', str(lst))
 
# Using set
Output = [int(i) for i in set(temp)]
 
# Printing output
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(). 






# Python code to convert list of tuples into
# list of all digits which exists
# in elements of list.
 
# Importing
from itertools import chain
 
# Input list initialization
lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
 
# using lambda
temp = map(lambda x: str(x), chain.from_iterable(lst))
 
# Output list initialization
Output = set()
 
# Adding element in Output
for x in temp:
    for elem in x:
        Output.add(elem)
 
# Printing output
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




# Python code to convert list of tuples into
# list of all digits which exists
# in elements of list.
 
 
# Input list initialization
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)))
 
# Printing output
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.




# Initialize the list
lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
 
# Extract the digits using a list comprehension and the isdigit method
digits = set()
 
# Print the resulting list
print("The list of digits:", digits)
#This code is contributed by Edula Vinay Kumar Reddy

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:

Below is the implementation of the above approach:




# Input list initialization
lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
 
# Using nested loops and string conversion
digits = []
for tup in lst:
    for item in tup:
        for char in str(item):
            if char.isdigit():
                digits.append(int(char))
 
# Using set to remove duplicates
Output = list(set(digits))
 
# Printing output
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 numpy as np
 
# Initialize the list
lst = [(11, 100), (22, 200), (33, 300), (44, 400), (88, 800)]
 
# Convert the list of tuples to a 1-D numpy array using ravel() method
arr = np.array(lst).ravel()
 
# Extract the digits using a list comprehension and the isdigit method
digits = [int(d) for d in ''.join(map(str, arr)) if d.isdigit()]
 
# Remove duplicates using set
digits = list(set(digits))
 
# Print the resulting list
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.


Article Tags :