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 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)) |
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)) |
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)) |
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
# 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)) |
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.
Contact Us