Open In App

Python | Difference between two lists

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In Python programming, comparing two lists is a common task with multiple approaches. This article explores different methods for obtaining the dissimilarity between two lists, enhancing your proficiency in handling lists and data comparison in Python. Join us on this insightful journey into various strategies for discerning differences between lists.

Example

Input:
list1 = [10, 15, 20, 25, 30, 35, 40]
list2 = [25, 40, 35] 
Output: [10, 15, 20, 30]
Explanation: resultant list = list1 - list2

 Note: When you have multiple same elements then this would not work. In that case, this code will simply remove the same elements.
In that case, you can maintain a count of each element in both lists.

Ways to Compare Two Lists in Python

There are various ways to compare two lists in Python. Here, we are discussing some generally used methods for comparing two lists in Python those are following.

  • Use “in” Method
  • Using List Comprehension
  • Use set() Function
  • Use Numpy
  • Using zip() Function
  • Count occurrences using Counter

Python “in” keyword to Compare Two Lists in Python

In this example code iterates through elements in list `li1`, and appends each element to `temp3` if it is not present in list `li2`. The final result in `temp3` contains elements from `li1` that are not present in `li2`.

Python3




li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
 
temp3 = []
for element in li1:
    if element not in li2:
        temp3.append(element)
 
print(temp3)


Output

[10, 15, 20, 30]

Difference Between Two Lists in Python Using a List comprehension

In this example code creates a set ‘s’ from the elements of list ‘li2’, and then generates a new list ‘temp3’ containing elements from list ‘li1’ that are not present in set ‘s’. Finally, it prints the elements in ‘temp3’.

Python3




li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
 
s = set(li2)
temp3 = [x for x in li1 if x not in s]
print(temp3)


Output

[10, 15, 20, 30]

Find the Difference Between Two Lists in Python using set()

In this method, we convert the lists into sets explicitly and then simply reduce one from the other using the subtract operator. For more references on set visit Sets in Python. It is a similar technique that we used previously. The only difference is, that we replaced the nested loops with the list comprehension syntax.

Python3




li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
 
s = set(li2)
temp3 = [x for x in li1 if x not in s]
print(temp3)


Output

[10, 15, 20, 30]

Use Numpy to Compare Two Lists in Python

The numpy.concatenate() function concatenate a sequence of arrays along an existing axis. In this example code uses NumPy to create arrays `li1` and `li2`, finds the set differences between them (`dif1` and `dif2`), and concatenates these differences into a single list (`temp3`), finally printing the result.

Python3




import numpy as np
li1 = np.array([10, 15, 20, 25, 30, 35, 40])
li2 = np.array([25, 40, 35])
 
dif1 = np.setdiff1d(li1, li2)
dif2 = np.setdiff1d(li2, li1)
 
temp3 = np.concatenate((dif1, dif2))
print(list(temp3))


Output

[10, 15, 20, 30]

Compare Two Lists in Python Using zip() Function

In this example code compares corresponding elements of two lists, li1 and li2, and creates a list of boolean values indicating whether the elements are equal. The `all` function checks if all elements in the result list are True.

Python3




li1 = [10, 15, 20]
li2 = [25, 40, 35]
 
result = [a == b for a, b in zip(li1, li2)]
print(all(result))


Output

False

Compare Two Lists Using Count Occurrences Using Counter

In this example code uses the `Counter` class from the `collections` module to create frequency counters for two lists, `li1` and `li2`. It then compares the counters to check if the lists have the same elements with the same frequencies, assigning the result to `are_lists_equal`.

Python3




from collections import Counter
 
li1 = [10, 15, 20, 25, 30, 35, 40]
li2 = [25, 40, 35]
 
counter1 = Counter(li1)
counter2 = Counter(li2)
 
are_lists_equal = counter1 == counter2
print(are_lists_equal)


Output

False


Last Updated : 29 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads