Sometimes, while working with data, we may have a problem in which we require to find the unmatching records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension List comprehension can be opted as method to perform this task in one line rather than running a loop to find the common element. In this, we just iterate for single list and check if any element occurs in other one.
Python3
# Python3 code to demonstrate working of # Mutually different Records # Using list comprehension # Initializing lists test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )] test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Mutually different Records # Using list comprehension res1 = [ele1 for ele1 in test_list1 for ele2 in test_list2 if ele1 = = ele2] res = [ele for ele in test_list1 if ele not in res1] res2 = [ele for ele in test_list2 if ele not in res1] rest = res2 + res # printing result print ("The unmatched data records are : " + str (rest)) |
The original list 1 is : [('gfg', 1), ('is', 2), ('best', 3)] The original list 2 is : [('i', 3), ('love', 4), ('gfg', 1)] The unmatched data records are : [('i', 3), ('love', 4), ('is', 2), ('best', 3)]
The time complexity of the given code is O(n^2) because the program uses two nested loops.
The space complexity of the given code is O(n) because the space required for storing the res1, res, res2 and rest lists depend on the length of the input lists.
Method #2 : Using set.symmetric_difference()
This task can also be performed in smaller way using the generic set symmetric difference. In this, we first convert the list of records to a set and then perform its unmatching using symmetric_difference().
Python3
# Python3 code to demonstrate working of # Mutually different Records # Using set.symmetric_difference() # Initializing lists test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )] test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Mutually different Records # set.symmetric_difference() res = list ( set (test_list1).symmetric_difference( set (test_list2))) # printing result print ("The unmatched data records are : " + str (res)) |
The original list 1 is : [('gfg', 1), ('is', 2), ('best', 3)] The original list 2 is : [('i', 3), ('love', 4), ('gfg', 1)] The unmatched data records are : [('i', 3), ('love', 4), ('is', 2), ('best', 3)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method3 : Using a dictionary to keep track of the occurrences of each record in both lists.
This approach works by iterating over both lists and updating a dictionary with the occurrences of each record. Then, it selects only the records that occur exactly once, which correspond to the mutually different records.
Python3
# Initializing lists test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )] test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )] # Create dictionary with record occurrences in both lists record_counts = {} for record in test_list1 + test_list2: if record in record_counts: record_counts[record] + = 1 else : record_counts[record] = 1 # Find mutually different records mutually_different = [record for record, count in record_counts.items() if count = = 1 ] # Printing result print ( "The mutually different records are: " + str (mutually_different)) |
The mutually different records are: [('is', 2), ('best', 3), ('i', 3), ('love', 4)]
Time complexity: O(n), where n is the total number of tuples in both lists,
Auxiliary space: O(n), because it creates a dictionary to store the record occurrences, which could potentially store all n tuples if none of them are the same.