Python – Extract tuple supersets from List
Last Updated :
17 May, 2023
Sometimes, while working with Python tuples, we can have a problem in which we need to extract all the tuples, which have all the elements in target tuple. This problem can have application in domains such as web development. Let’s discuss certain way in which this problem can be solved.
Input : test_list = [(5, 6, 6), (4, 2, 7), (9, 6, 5, 6)] test_tuple = (6, 6) Output : [(5, 6, 6), (9, 6, 5, 6)] Input : test_list = [(5, 6, 6), (4, 2, 6), (9, 6, 5, 6)] test_tuple = (6, ) Output : [(5, 6, 6), (4, 2, 6), (9, 6, 5, 6)]
Method : Using Counter() + list comprehension + all() The combination of above functions can be used to solve this problem. In this, we perform the task of counting using Counter(). The all(), is used to check if all elements form the subset and list comprehension is used to bind all the logic in one block.
Python3
from collections import Counter
test_list = [( 5 , 6 , 8 ), ( 4 , 2 , 7 ), ( 9 , 6 , 5 , 6 )]
print ("The original list is : " + str (test_list))
test_tup = ( 6 , 6 , 5 )
res = [sub for sub in test_list if
all (Counter(sub)[x] > = Counter(test_tup)[x]
for x in Counter(test_tup))]
print ("The superset tuples : " + str (res))
|
Output :
The original list is : [(5, 6, 8), (4, 2, 7), (9, 6, 5, 6)]
The superset tuples : [(9, 6, 5, 6)]
Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The Counter() + list comprehension + all() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), the algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method#2 : Using filter() Function
Approach
Iterate through each tuple in the list.Check if the test tuple is a subset of the current tuple using the set() method.
If it is a subset, add the current tuple to a new list, which will contain all the supersets of the test tuple. Return the new list of supersets
Algorithm
1. Initialize a new empty list called result.
2. Loop through each tuple in the given list.
3. If the test tuple is a subset of the current tuple, append the current tuple to the result list.
4. Return the result list.
Python3
def extract_supersets(test_list, test_tuple):
return list ( filter ( lambda t: set (t).issuperset( set (test_tuple)), test_list))
test_list = [( 5 , 6 , 6 ), ( 4 , 2 , 7 ), ( 9 , 6 , 5 , 6 )]
test_tuple = ( 6 , 6 )
print (extract_supersets(test_list, test_tuple))
|
Output
[(5, 6, 6), (9, 6, 5, 6)]
Time complexity: O(n*m) where n is the length of the list and m is the length of the longest tuple in the list. This is because we need to loop through each tuple in the list, and also need to check if the test tuple is a subset of each tuple, which takes O(m) time.
Space complexity: O(k) where k is the number of supersets found. This is because we need to create a new list to store the supersets.
Method #3:Using for loop
Algorithm
- Define a function called extract_supersets which takes two arguments: test_list and test_tuple.
- Inside the function, initialize an empty list called supersets.
- Loop through each tuple in test_list.
- Check if the set of the tuple is a superset of the set of test_tuple using the issuperset() method.
- If it is a superset, append the tuple to the supersets list.
- Return the supersets list.
- Call the extract_supersets() function with the given test_list and test_tuple as arguments.
- Print the result of the function call.
Python3
def extract_supersets(test_list, test_tuple):
result = []
for t in test_list:
if set (test_tuple).issubset( set (t)):
result.append(t)
return result
test_list = [( 5 , 6 , 6 ), ( 4 , 2 , 7 ), ( 9 , 6 , 5 , 6 )]
test_tuple = ( 6 , 6 )
print (extract_supersets(test_list, test_tuple))
|
Output
[(5, 6, 6), (9, 6, 5, 6)]
The time complexity of this code is O(n*k), where n is the length of test_list and k is the length of the largest tuple in test_list. This is because we are iterating over each tuple in test_list and performing a set operation that takes O(k) time.
The space complexity of this code is O(1) since we are not using any additional space beyond the input and output lists.
Approach#4: using subset + len
We use filter function with a lambda function to filter out the tuples in the list which are the supersets of the given tuple.
Algorithm
1. Define the input list and tuple.
2. Use filter function with lambda to filter out the tuples which are supersets of the given tuple.
3. Convert the filtered tuples to list.
4. Return the list.
Python3
import itertools
test_list = [( 5 , 6 , 6 ), ( 4 , 2 , 7 ), ( 9 , 6 , 5 , 6 )]
test_tuple = ( 6 , 6 )
result_list = list ( filter ( lambda x: set (test_tuple).issubset(x) and len (x) > len (test_tuple), test_list))
print (result_list)
|
Output
[(5, 6, 6), (9, 6, 5, 6)]
Time complexity: O(n*m), where n is the length of the input list and m is the length of the longest tuple in the list.
Auxiliary Space: O(1) (excluding the space required to store the result list).
Similar Reads
Python - Extract digits from Tuple list
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 : t
6 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python | Test if tuple is distinct
Sometimes, while working with records, we have a problem in which we need to find if all elements of tuple are different. This can have applications in many domains including web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is a brute force
4 min read
Python - Extract Similar pairs from List
Sometimes, while working with Python lists, we can have a problem in which we need to perform extraction of all the elements pairs in list. This kind of problem can have application in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be perfo
5 min read
Python - Extract Symmetric Tuples
Sometimes while working with Python tuples, we can have a problem in which we need to extract all the pairs which are symmetric, i.e for any (x, y), we have (y, x) pair present. This kind of problem can have application in domains such as day-day programming and web development. Let's discuss certai
4 min read
Convert List to Tuple in Python
The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Python - Tuple key detection from value list
Sometimes, while working with record data, we can have a problem in which we need to extract the key which has matching value of K from its value list. This kind of problem can occur in domains that are linked to data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
6 min read
Python - Extract Preceding Record from list values
Sometimes, while working with Tuple records, we can have a problem in which we need to extract the record, which is preceding to particular key provided. This kind of problem can have application in domains such as web development and day-day programming. Let's discuss certain ways in which this tas
9 min read
Python - Extract Elements from Ranges in List
We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]]. Using List ComprehensionLis
3 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read