Related Articles

Related Articles

Python | Difference in Record Lists
  • Last Updated : 27 Feb, 2020

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :