Compare and get Differences between two Lists in Python

Lists in Python:

Lists are the most versatile ordered list object type in Python. It’s also known as a sequence, which is an ordered group of objects that can contain objects of any data form, including Python Numbers, Python Strings, and nested lists. One of the most widely used and flexible Python Data Types is the list.

You can check if two lists are equal python.

Examples:

Input:

list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]

Output:

Printing the Differences between the lists : 
['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']

Compare and get Differences between two Lists in Python

Let’s say we have two lists

There may be certain items in the first list that are not present in the second list. There are also several items that are present in the second list but not in the first list. We’d like to compare our two lists to figure out what the variations are.

There are several ways to compare and get differences between two lists some of them are:

Method #1:Using union function in sets

When we make a set from a list, it only includes the list’s unique elements. So, let’s transform our lists into sets, and then subtract these sets to find the differences.
We discovered the variations between the two lists, i.e. elements that are present in one list but not in the other.

Below is the implementation:

# given two lists
list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]
# converting two lists to sets
setlist1 = set(list1)
setlist2 = set(list2)
# getting the differences in both lists
listDif = (setlist1 - setlist2).union(setlist2 - setlist2)
print('Printing the Differences between the lists : ')
print(listDif)

Output:

Printing the Differences between the lists : 
['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']

Method #2:Using set.difference()

Instead of subtracting two sets with the – operator in the previous solution, we can get the differences by using the set difference() feature.

So, let’s convert our lists to sets, and then use the difference() function to find the differences between two lists.

Below is the implementation:

# given two lists
list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]
# converting two lists to sets
setlist1 = set(list1)
setlist2 = set(list2)
# getting elements in first list which are not in second list
difference1 = setlist1.difference(setlist2)
# getting elements in second list which are not in first list
difference2 = setlist2.difference(setlist1)
listDif = difference1.union(difference2)
print('Printing the Differences between the lists : ')
print(listDif)

Output:

Printing the Differences between the lists : 
['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']

Method #3:Using List Comprehension

To find the differences, we can iterate over both lists and search for elements in other lists. However, we can use list comprehension for iteration.

Below is the implementation:

# given two lists
list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]

# getting elements in first list which are not in second list
difference1 = [element for element in list1 if element not in list2]
# getting elements in second list which are not in first list
difference2 = [element for element in list2 if element not in list1]
listDif = difference1+difference2
print('Printing the Differences between the lists : ')
print(listDif)

Output:

Printing the Differences between the lists : 
['Geeks', 'is', 'BTechGeeks', 'online', 'Platform', 'world', 'Python', 'Coding', 'Language']

Method #4:Using set.symmetric_difference()

We had all of the variations between two lists in two steps in all of the previous solutions. Using symmetric difference(), however, we can accomplish this in a single stage.
Set has a member function called symmetric difference() that takes another sequence as an argument. It returns a new set containing elements from either the calling set object or the sequence statement, but not both. In other words, it returns the differences between set and list. Let’s see if we can use this to determine the differences between two lists.

Below is the implementation:

# given two lists
list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]

listDif = set(list1).symmetric_difference(list2)
print('Printing the Differences between the lists : ')
print(listDif)

Output:

Printing the Differences between the lists : 
{'is', 'online', 'world', 'BTechGeeks', 'Python', 'Language', 'Coding', 'Geeks', 'Platform'}

Method #5:Using set and ^ operator

Quick approach for solving this problem is to use ^ and sets.

Below is the implementation:

# given two lists
list1 = ["Hello", "Geeks", "this", "is", "BTechGeeks", "online", "Platform"]
list2 = ["Hello", "world", "this", "Python", "Coding", "Language"]

listDif = set(list1) ^ set(list2)
print('Printing the Differences between the lists : ')
print(listDif)

Output:

Printing the Differences between the lists : 
{'is', 'online', 'world', 'BTechGeeks', 'Python', 'Language', 'Coding', 'Geeks', 'Platform'}