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
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 = [ele1 for ele1 in test_list1 for ele2 in test_list2 if ele1 = = ele2]
res = list ( set (res) ^ set (test_list1))
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
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).intersection( set (test_list2)))
res = list ( set (res) ^ set (test_list1))
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
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 ( filter ( lambda x: x not in set (test_list2), set (test_list1)))
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), 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
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))
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)
print ( "The difference of data records is : " + str (diff_list))
|
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 : [('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
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
Column Average in Record List - Python
Given a list of records where each record contains multiple fields, the task is to compute the average of a specific column. Each record is represented as a dictionary or a list, and the goal is to extract values from the chosen column and calculate their average. Letâs explore different methods to
3 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 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
Python - K difference index pairing in list
Sometimes while programming, we can face a problem in which we need to perform K difference element concatenation. This problem can occur at times of school programming or competitive programming. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension +
5 min read
Python | Difference of two lists including duplicates
We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4]. Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read