Python | Difference in Record Lists
Last Updated :
05 May, 2023
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 comprehension
List comprehension can be opted as method to perform this task in one line rather than running a loop to find the difference elements. In this, we just iterate for single list and check if any element occurs in other one.
Python3
# Python3 code to demonstrate working of
# Difference in Record Lists
# 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))
# Intersection in Tuple Records Data
# Using list comprehension
res = [ele1 for ele1 in test_list1 for ele2 in test_list2 if ele1 == ele2]
res = list(set(res) ^ set(test_list1))
# Printing result
print("The difference of data records is : " + 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 difference of data records is : [('best', 3), ('is', 2)]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n*n), where n is the number of elements in the list “test_list”.
Method #2: Using set.intersection() + set operations
This task can also be performed in a smaller way using the generic set intersection. In this, we first convert the list of records to a set and then perform its intersection using intersection(). Then the result is computed by taking the uncommon element of the result from the first list.
Python3
# Python3 code to demonstrate working of
# Difference in Record Lists
# Using set.intersection() + set operations
# 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))
# Difference in Record Lists
# Using set.intersection() + set operations
res = list(set(test_list1).intersection(set(test_list2)))
res = list(set(res) ^ set(test_list1))
# printing result
print("The difference of data records is : " + 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 difference of data records is : [('best', 3), ('is', 2)]
Method #3: Here is another approach to finding the difference between two lists of records, using the filter() method.
Python3
# Python3 code to demonstrate working of
# Difference in Record Lists
# Using filter() method
# 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))
# Difference in Record Lists
# Using filter() method
res = list(filter(lambda x: x not in set(test_list2), set(test_list1)))
# printing result
print("The difference of data records is : " + str(res))
OutputThe original list 1 is : [('gfg', 1), ('is', 2), ('best', 3)]
The original list 2 is : [('i', 3), ('love', 4), ('gfg', 1)]
The difference of data records is : [('best', 3), ('is', 2)]
Time Complexity: O(n), where n is the length of the test_list1
Auxiliary Space: O(n), as a new list res of length n is created.
Method 4: use a for loop and conditional statements.
Steps:
- Initialize two lists of records.
- Create an empty list to store the difference.
- Use a for loop to iterate through each record in the first list.
- Use a nested for loop to iterate through each record in the second list.
- Check if the record in the first list matches the record in the second list.
- If the records match, break out of the nested loop and move to the next record in the first list.
- If the records do not match, append the record to the difference list and move to the next record in the first list.
- Print the difference list.
Python3
# Python3 code to demonstrate working of
# Difference in Record Lists
# Using for loop and conditional statements
# 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))
# Difference in Record Lists
# Using for loop and conditional statements
diff_list = []
for record1 in test_list1:
found_match = False
for record2 in test_list2:
if record1 == record2:
found_match = True
break
if not found_match:
diff_list.append(record1)
# printing result
print("The difference of data records is : " + str(diff_list))
OutputThe original list 1 is : [('gfg', 1), ('is', 2), ('best', 3)]
The original list 2 is : [('i', 3), ('love', 4), ('gfg', 1)]
The difference of data records is : [('is', 2), ('best', 3)]
Time Complexity: O(n*m), where n and m are the lengths of the two lists respectively.
Auxiliary Space: O(k), where k is the length of the difference list.
Similar Reads
Python | Triple list difference
The difference between 2 lists have been dealt previously, but sometimes, we can have more than two lists and we need to find the mutual differences of one list with every other list. This kind of problem has applications in many domains. Let's discuss certain ways in which this problem can be solve
10 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
Python | Record Point with Minimum difference
Sometimes, while working with data, we might have a problem in which we need to find minimum difference between available pairs in list. This can be application to many problems in mathematics domain. Letâs discuss certain ways in which this task can be performed.Method #1 : Using min() + list compr
2 min read
Python | Mutually different Records
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
4 min read
Python | Records List Concatenation
Sometimes, while working with Python records, we can have a problem in which we need to perform cross concatenation of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
4 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 | 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 | Set Difference in list of dictionaries
The difference of two lists have been discussed many times, but sometimes we have a large number of data and we need to find the difference i.e the elements in dict2 not in 1 to reduce the redundancies. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension The na
3 min read
Python - Nested Records List from Lists
Sometimes, while working with Python Data, we can have problems in which we have data incoming in different formats. In this, we can receive data as key and value in separate dictionaries and we are required to make values as list of records with a new key. Let's discuss certain ways in which we can
6 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