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
# 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))
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
# 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))
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
# 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))
OutputThe 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.
Similar Reads
Python | Difference in Record Lists Sometimes, while working with data, we may have a problem in which we require to find the difference 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
5 min read
Python | Records Intersection Sometimes, while working with tuples, we can have a problem in which we need similar features of two records. This type of application can come in the Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1: Using set() + "&" operator This task can be perfo
6 min read
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
7 min read
Python - Convert Uneven Lists into Records Sometimes, while working with Records, we can have a problem, that we have keys in one list and values in other. But sometimes, values can be multiple in order, like the scores or marks of particular subject. This type of problem can occur in school programming and development domains. Lets discuss
3 min read
Python Remove Set Items Python sets are an efficient way to store unique, unordered items. While adding items to a set is straightforward, removing items also offers a variety of methods. This article will guide you through the different techniques available to remove items from a set in Python.Remove single set item using
3 min read
Difference between two Lists in Python The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most
3 min read