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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 #2 : Using set.intersection() + set operations
This task can also be performed in 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 uncommon element of result from first list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.