Python | Mutually different Records
Last Updated :
09 Apr, 2023
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
test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )]
test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
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
print ("The unmatched data records are : " + str (rest))
|
Output :
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
test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )]
test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = list ( set (test_list1).symmetric_difference( set (test_list2)))
print ("The unmatched data records are : " + str (res))
|
Output :
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
test_list1 = [( 'gfg' , 1 ), ( 'is' , 2 ), ( 'best' , 3 )]
test_list2 = [( 'i' , 3 ), ( 'love' , 4 ), ( 'gfg' , 1 )]
record_counts = {}
for record in test_list1 + test_list2:
if record in record_counts:
record_counts[record] + = 1
else :
record_counts[record] = 1
mutually_different = [record for record, count in record_counts.items() if count = = 1 ]
print ( "The mutually different records are: " + str (mutually_different))
|
Output
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.
Share your thoughts in the comments
Please Login to comment...