Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Extract digits from Tuple list

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 21 Feb, 2023
Improve Article
Save Article

Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the digits from tuple list. This kind of problem can find its application in data domains and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(15, 3), (3, 9)] 
Output : [9, 5, 3, 1]

Input : test_list = [(15, 3)] 
Output : [5, 3, 1] 

Method #1: Using map() + chain.from_iterable() + set() + loop 
The combination of above functions can be used to solve this problem. In this, we perform the task of flattening list using chain.from_iterable(), and then the digits are extracted using brute method. set() is used to remove duplicate digits.

Python3




# Python3 code to demonstrate working of
# Extract digits from Tuple list
# Using map() + chain.from_iterable() + set() + loop
from itertools import chain
 
# initializing list
test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract digits from Tuple list
# Using map() + chain.from_iterable() + set() + loop
temp = map(lambda ele: str(ele), chain.from_iterable(test_list))
res = set()
for sub in temp:
    for ele in sub:
        res.add(ele)
 
# printing result
print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : {'1', '0', '3', '2', '9', '5'}

Time Complexity: O(n*m), where n is the length of the input list and m is the maximum length of any tuple element in the list.
Auxiliary Space: O(k), where k is the number of unique digits in the input list. This is because the set data structure is used to store the extracted digits.

Method #2: Using regex expression 
This is yet another way in which this task can be performed. In this, an appropriate regex expression is used to extract the required unique digits.

Python3




# Python3 code to demonstrate working of
# Extract digits from Tuple list
# Using regex expression
import re
 
# initializing list
test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Extract digits from Tuple list
# Using regex expression
temp = re.sub(r'[\[\]\(\), ]', '', str(test_list))
res = [int(ele) for ele in set(temp)]
 
# printing result
print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : [5, 9, 2, 0, 1, 3]

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n), where n is the length of the input list.

Method #3: Using list(),str(),map(),set() methods .

Initially converted all elements of tuple to string and concatenated them.Later used set() method to remove the duplicates, converted string elements to integer elements and finally converted them to list datatype.

Python3




# Python3 code to demonstrate working of
# Extract digits from Tuple list
 
# initializing list
test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]
 
# printing original list
print("The original list is : " + str(test_list))
x=""
# Extract digits from Tuple list
for i in test_list:
    for j in i:
        x+=str(j)
res=list(map(int,set(x)))
# printing result
print("The extracted digits : " + str(res))

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : [2, 3, 0, 1, 9, 5]

Method#4: Using list Comprehension

Python3




# Initializing list
test_list = [(15, 3), (3, 9), (1, 10), (99, 2)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Extracting digits from Tuple list using list comprehensions
temp = ''.join([str(i) for sublist in test_list for i in sublist])
result = set(temp)
result = [int(i) for i in result]
# Printing result
print("The extracted digits : " + str(list(result)))
#This code is contributed by Vinay Pinjala.

Output

The original list is : [(15, 3), (3, 9), (1, 10), (99, 2)]
The extracted digits : ['0', '9', '1', '3', '5', '2']

Time complexity : O(n)
Auxiliary Space : O(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!