Python Find Difference of Two Lists

Python find difference between two lists; Through this tutorial, i am going to show you how to find difference between two lists in python.

Python Find Difference of Two Lists

  • Python Program to find difference of two lists using Set()
  • Python Program to find difference of two lists using for loop

Python Program to find difference of two lists using Set()

# list1 and list2
list1 = [200, 400, 300, 80, 90]
list2 = [200, 400, 300, 70, 100]
# print both the list
print("list1:", list1)
print("list2:", list2)
# finding and printing differences of the lists
print("Difference elements:")
print(list (set(list1) - set (list2)))

After executing the program, the output will be:

list1: [200, 400, 300, 80, 90]
list2: [200, 400, 300, 70, 100]
Difference elements:
[80, 90]

Python Program to find difference of two lists using for loop

# list1 and list2
list1 = [200, 400, 300, 80, 90]
list2 = [200, 400, 300, 70, 100]
list_difference = []
for item in list1:
  if item not in list2:
    list_difference.append(item)
print(list_difference)

After executing the program, the output will be:

[80, 90]

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *