Python – Paired Existence in Records
Sometimes, while working with Python records, we can have a problem in which we need to check for paired existence inside the record, or else if one doesn’t exist, other also should not. This kind of problem is common in domains such as Data Science and web development. Let’s discuss certain ways in which this task can be performed.
Input :
test_list = [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
pairs = (‘is’, ‘good’)
Output : [(‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
Input :
test_list = [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘better’)]
pairs = (‘better’, ‘good’)
Output : []
Method #1 : Using generator expression
This is brute force way in which this task can be performed. In this, we check for the existence/non-existence of both numbers and accept the result if, none or both are present.
Python3
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
print ( "The original list is : " + str (test_list))
pairs = ( 'Gfg' , 'Best' )
res = []
for sub in test_list:
if ((pairs[ 0 ] in sub and pairs[ 1 ] in sub) or (
pairs[ 0 ] not in sub and pairs[ 1 ] not in sub)):
res.append(sub)
print ( "The resultant records : " + str (res))
|
Output :
The original list is : [(‘Gfg’, ‘is’, ‘Best’), (‘Gfg’, ‘is’, ‘good’), (‘CS’, ‘is’, ‘good’)]
The resultant records : [(‘Gfg’, ‘is’, ‘Best’), (‘CS’, ‘is’, ‘good’)]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(m), where m is the number of elements that satisfy the given condition.
Method #2 : Using XNOR
This is yet another way to solve this problem. In this, use the power of XOR operator to perform this task and negate the result.
Python3
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
print ( "The original list is : " + str (test_list))
pairs = ( 'Gfg' , 'Best' )
res = []
for sub in test_list:
if ( not ((pairs[ 0 ] in sub) ^ (pairs[ 1 ] in sub))):
res.append(sub)
print ( "The resultant records : " + str (res))
|
Output :
The original list is : [('Gfg', 'is', 'Best'), ('Gfg', 'is', 'good'), ('CS', 'is', 'good')]
The resultant records : [('Gfg', 'is', 'Best'), ('CS', 'is', 'good')]
The time complexity of this code is O(n*m), where n is the length of the test_list and m is the length of each tuple in the test_list.
The space complexity of this code is O(k), where k is the number of tuples that satisfy the given condition.
Method #3 : Using filter() and lambda function
Use the filter() function along with a lambda function to filter out the tuples that don’t contain both the pairs.
1. Define a function named find_pairs that takes two arguments – test_list and pairs.
2. In the function, use the filter() function along with a lambda function to filter out the tuples that don’t contain both the pairs.
3. The lambda function takes one argument – x, which represents a tuple from the test_list.
4. Use the all() function to check if both pairs exist in the tuple x.
5. Convert the filter object to a list and store it in a variable named output.
6. Return the output list.
Python3
def find_pairs(test_list, pairs):
output = list ( filter ( lambda x: all (pair in x for pair in pairs), test_list))
return output
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
pairs = ( 'Gfg' , 'Best' )
print (find_pairs(test_list, pairs))
|
Output
[('Gfg', 'is', 'Best')]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the length of the pairs tuple.
Auxiliary Space: O(k), where k is the number of tuples that satisfy the condition.
Method #4: Using List Comprehension and Set Intersection
Use list comprehension along with set intersection to achieve the same result. The idea is to convert each sub-list to a set and then take the intersection of the set with the set containing the given pairs. If the resulting set is equal to the set containing the pairs or the intersection set is empty, then we add that sub-list to the result.
Python3
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
pairs = ( 'Gfg' , 'Best' )
pair_set = set (pairs)
res = [sub for sub in test_list if pair_set.intersection( set (sub)) = = pair_set or pair_set.intersection( set (sub)) = = set ()]
print ( "The resultant records : " + str (res))
|
Output
The resultant records : [('Gfg', 'is', 'Best'), ('CS', 'is', 'good')]
Time Complexity: O(N*M), where N is the length of the given list and M is the maximum length of any sub-list.
Auxiliary Space: O(M), where M is the maximum length of any sub-list.
Method #5: Using a for loop
Iterate over each tuple in the list and check if the given pairs exist in it or not. If it exists, we can append it to the result list.
Step-by-step approach:
- Initialize an empty list to store the resulting tuples.
- Iterate over each tuple in the input list.
- Check if the given pairs exist in the tuple or not. We can use the “in” operator to check if an element exists in a tuple.
- If the pairs exist in the tuple, append it to the result list.
- Return the result list.
Below is the implementation of the above approach:
Python3
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
pairs = ( 'Gfg' , 'Best' )
result = []
for record in test_list:
if all (pair in record for pair in pairs):
result.append(record)
print ( "The resultant records : " + str (result))
|
Output
The resultant records : [('Gfg', 'is', 'Best')]
Time complexity: O(n*m), where n is the length of the given list and m is the length of each record in the list.
Auxiliary space: O(k), where k is the number of records that satisfy the given condition
Method 6: Using the built-in function “set” and set intersection
Step-by-step approach:
- Initialize the input list “test_list” and the pairs “pairs”.
- Use the built-in function “set” to convert the pairs tuple into a set.
- Use the “issubset” method of the set object to check if the pairs set is a subset of each record in the input list.
- Use the “filter” function with a lambda function to filter out the records that do not contain the pairs set as a subset.
- Convert the filtered records into a list using the “list” function.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
test_list = [( 'Gfg' , 'is' , 'Best' ),
( 'Gfg' , 'is' , 'good' ),
( 'CS' , 'is' , 'good' )]
pairs = ( 'Gfg' , 'Best' )
result = list ( filter ( lambda record: set (pairs).issubset( set (record)), test_list))
print ( "The resultant records : " + str (result))
|
Output
The resultant records : [('Gfg', 'is', 'Best')]
Time complexity: O(nm), where n is the length of the input list and m is the length of the pairs tuple.
Auxiliary space: O(1), only a constant amount of extra space is needed to store the intermediate set object.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...