How to Find the Common Items in Two Lists in Python

Published February 05, 2025

How to Find the Common Items in Two Lists in Python How to Find the Common Items in Two Lists in Python

In Python, there is no method or function to find the common items in two lists. This blog post will show you different ways of doing it.

Create two lists a and b

For our example, we will create lists a and b having a few common items and a few different items.

a = ['apple', 'orange', 'mango', 'strawberry', 'papaya', 'banana']
b = ['durian', 'apple', 'orange', 'mango', 'papaya', 'banana']

Find the common items using set() and &

There are no list functions to find the common items, but there is a set operator & that can be used to find the common items, so you can convert the lists to sets and perform the & operator between the sets. The operator & is also the intersection operation.

We will create list c which contains the intersection of set(a) and set(b)

Intersection of a and b

c = list(set(a) & set(b))
print(c)

Output:

['apple', 'papaya', 'mango', 'orange', 'banana']

Full Python code

a = ['apple', 'orange', 'mango', 'strawberry', 'papaya', 'banana']
b = ['durian', 'apple', 'orange', 'mango', 'papaya', 'banana']

c = list(set(a) & set(b))
print(c)

Output:

['apple', 'papaya', 'mango', 'orange', 'banana']

Why is the resulting list c not ordered?

The set() function does not retain the order. Every time you create a set from a list, or from anything, the items in the set can appear anywhere randomly.

Conclusion

If you found this blog post useful, feel free to share it. Thanks for reading.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: February 05, 2025.     This post was originally written on February 04, 2025.