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 - Symmetric Difference of Multiple sets
Symmetric Differences among groups of sets are elements that belong to any one of the sets but are not present in any other set. Given a list of sets and the task is to write a Python program to get the symmetric difference of the same. Input : test_list = [{5, 3, 2, 6, 1}, {7, 5, 3, 8, 2}, {9, 3},
3 min read
Python - Row with Maximum Record Element
Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
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 | Intersection in Tuple Records Data
Sometimes, while working with data, we may have a problem in which we require to find the matching records between two lists that we receive. This is a very common problem and records usually occur as a tuple. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list com
7 min read
Concatenate All Records - Python
The task of concatenating all records in Python involves combining elements from a list, typically strings, into a single unified string. The goal is to concatenate each individual element, ensuring that the result is a continuous string without spaces or delimiters, unless specified. For example, g
3 min read
Python - Fill gaps in consecutive Records
Sometimes, while working with Python records, we can have a problem in which we have consecutive records, but a few missing and needs to be filled with any constant K. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which we need to perform
3 min read
Python | Group and count similar records
Sometimes, while working with records, we can have a problem in which we need to collect and maintain the counter value inside records. This kind of application is important in the web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + Counte
6 min read