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
# Python3 code to demonstrate working of # Paired Existence in Records # Using generator expression # initializing list test_list = [( 'Gfg' , 'is' , 'Best' ), ( 'Gfg' , 'is' , 'good' ), ( 'CS' , 'is' , 'good' )] # printing original list print ( "The original list is : " + str (test_list)) # initializing Pairs pairs = ( 'Gfg' , 'Best' ) # Paired Existence in Records # Using generator expression 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) # printing result print ( "The resultant records : " + str (res)) |
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
# Python3 code to demonstrate working of # Paired Existence in Records # Using XNOR # initializing list test_list = [( 'Gfg' , 'is' , 'Best' ), ( 'Gfg' , 'is' , 'good' ), ( 'CS' , 'is' , 'good' )] # printing original list print ( "The original list is : " + str (test_list)) # initializing Pairs pairs = ( 'Gfg' , 'Best' ) # Paired Existence in Records # Using XNOR res = [] for sub in test_list: if ( not ((pairs[ 0 ] in sub) ^ (pairs[ 1 ] in sub))): res.append(sub) # printing result print ( "The resultant records : " + str (res)) |
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)) |
[('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
# initializing list test_list = [( 'Gfg' , 'is' , 'Best' ), ( 'Gfg' , 'is' , 'good' ), ( 'CS' , 'is' , 'good' )] # initializing Pairs pairs = ( 'Gfg' , 'Best' ) # Paired Existence in Records # Using list comprehension and set intersection 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 ()] # printing result print ( "The resultant records : " + str (res)) |
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
# initializing list test_list = [( 'Gfg' , 'is' , 'Best' ), ( 'Gfg' , 'is' , 'good' ), ( 'CS' , 'is' , 'good' )] # initializing Pairs pairs = ( 'Gfg' , 'Best' ) # Paired Existence in Records # Using for loop result = [] for record in test_list: if all (pair in record for pair in pairs): result.append(record) # printing result print ( "The resultant records : " + str (result)) |
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
# initializing list test_list = [( 'Gfg' , 'is' , 'Best' ), ( 'Gfg' , 'is' , 'good' ), ( 'CS' , 'is' , 'good' )] # initializing Pairs pairs = ( 'Gfg' , 'Best' ) # Paired Existence in Records using set intersection result = list ( filter ( lambda record: set (pairs).issubset( set (record)), test_list)) # printing result print ( "The resultant records : " + str (result)) |
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.