close

How to get the Difference Between Two Lists in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about How to get the Difference Between Two Lists in Python. so without wasting time lets learn about of this.

How to get the Difference Between Two Lists in Python

  1. get the Difference Between Two Lists in Python

    to get the Difference Between Two Lists in Python just Use set.difference(). By using set.difference() you can get the Difference Between Two Lists in Python. This function eliminates the common elements from two sets. Lets learn about of this by given below example: list1 = [1,2,3,4,5,8,9] list2 = [1,2,3,4,5,6,7] difference_1 = set(list1).difference(set(list2)) difference_2 = set(list2).difference(set(list1)) list_difference = list(difference_1.union(difference_2)) print(list_difference) Output : [8, 9, 6, 7]

  2. How to get the Difference Between Two Lists in Python

    to get the Difference Between Two Lists in Python just Use set.symetric_difference(). By using set.symetric_difference() you can get the Difference Between Two Lists in Python. This function return the elements which are either in set1 or in set2. It will not return the common elements of both the set. Lets learn about of this by given below example: list1 = [1,2,3,4,5,8,9] list2 = [1,2,3,4,5,6,7] set_difference = set(list1).symmetric_difference(set(list2)) list_difference = list(set_difference) print(list_difference) Output : [6, 7, 8, 9]

  3. python list difference

    To get the Difference Between Two Lists in Python just Use set.symetric_difference(). By using set.symetric_difference() you can get the Difference Between Two Lists in Python. This function return the elements which are either in set1 or in set2. It will not return the common elements of both the set. Lets learn about of this by given below example: list1 = [1,2,3,4,5,8,9] list2 = [1,2,3,4,5,6,7] set_difference = set(list1).symmetric_difference(set(list2)) list_difference = list(set_difference) print(list_difference) Output : [6, 7, 8, 9]

Method 1: Use set.difference()

By using set.difference() you can get the Difference Between Lists. This function eliminates the common elements from two sets. Lets learn about of this by given below example:

list1 = [1,2,3,4,5,8,9]
list2 = [1,2,3,4,5,6,7]
difference_1 = set(list1).difference(set(list2))
difference_2 = set(list2).difference(set(list1))
list_difference = list(difference_1.union(difference_2))
print(list_difference)

Output :

[8, 9, 6, 7]

Method 2: Use set.symetric_difference()

By using set.symetric_difference() you can get the Difference Between Lists. This function return the elements which are either in set1 or in set2. It will not return the common elements of both the set. Lets learn about of this by given below example:

list1 = [1,2,3,4,5,8,9]
list2 = [1,2,3,4,5,6,7]
set_difference = set(list1).symmetric_difference(set(list2))
list_difference = list(set_difference)
print(list_difference)

Output :

[6, 7, 8, 9]

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment