Find Common Elements Between Two Lists in Python

To find common elements between two lists in Python, we can use various approaches such as the set() intersection, list comprehension, the filter() function, and loops.


Examples

1. Common Elements Between Two Lists Using set Intersection

The set data structure in Python provides a built-in method called intersection(), which helps find common elements between two lists efficiently.

</>
Copy
# Defining two lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

# Finding common elements using set intersection
common_elements = list(set(list1).intersection(set(list2)))

# Printing the result
print("Common Elements:", common_elements)

Explanation:

Here, we convert both lists into sets using set(list1) and set(list2). The intersection() method is used to find elements that are present in both sets. The result is then converted back into a list using list() to maintain the list format.

Output:

Common Elements: [4, 5]

2. Common Elements Between Two Lists Using List Comprehension

List comprehension offers a concise way to iterate through one list and check if each element exists in the second list.

</>
Copy
# Defining two lists
list1 = ["apple", "banana", "cherry"]
list2 = ["cherry", "orange", "banana"]

# Finding common elements using list comprehension
common_elements = [item for item in list1 if item in list2]

# Printing the result
print("Common Elements:", common_elements)

Explanation:

We iterate through list1 and check if each item exists in list2 using the if item in list2 condition. If the item is found, it is added to the resulting list.

Output:

Common Elements: ['banana', 'cherry']

3. Common Elements Between Two Lists Using filter() Function

The filter() function allows filtering elements that exist in both lists using a lambda function.

</>
Copy
# Defining two lists
list1 = [10, 20, 30, 40, 50]
list2 = [30, 40, 50, 60, 70]

# Finding common elements using filter()
common_elements = list(filter(lambda x: x in list2, list1))

# Printing the result
print("Common Elements:", common_elements)

Explanation:

The filter() function applies a lambda function that checks if an element from list1 exists in list2. The filtered result is then converted to a list.

Output:

Common Elements: [30, 40, 50]

4. Common Elements Between Two Lists Using a for Loop

Using a traditional for loop, we can manually check for common elements between the two lists.

</>
Copy
# Defining two lists
list1 = ["dog", "cat", "rabbit"]
list2 = ["lion", "cat", "rabbit"]

# Finding common elements using a for loop
common_elements = []
for item in list1:
    if item in list2:
        common_elements.append(item)

# Printing the result
print("Common Elements:", common_elements)

Explanation:

We iterate through list1 using a for loop and check if each element exists in list2. If it does, we append it to the common_elements list.

Output:

Common Elements: ['cat', 'rabbit']

Conclusion

There are multiple ways to find common elements between two lists in Python:

  1. set.intersection(): Efficient for large lists as it uses set operations.
  2. List Comprehension: A simple and readable approach.
  3. filter() Function: Uses functional programming for concise filtering.
  4. for Loop: A manual method suitable for beginners.

For performance efficiency, set.intersection() is the best choice, especially for large datasets. However, other methods provide readability and flexibility depending on the use case.