Javaexercise.com

Find Common Elements From Two Python Lists

In this topic, we are discussing for finding common elements in two different Python lists. It is common problem we usualy face during programming to find duplicate elements from list or find common or matching elements present in both the list. 

There are several ways by which we can check whether two lists has common elements or not. Here we are using some of them like: checking using list comprehension or using intersection() method of set etc. Lets see some examples.

Finding match items in List using list comprehension

This example shows the use of list comprehension technique to find match elements from two list. It returns a list that contains all the matched items.

# Take a list
list1 = [10,20,30,40,50]
# Print first list
print("list1 items:\n",list1)
# Take another list
list2 = [10,15,20,25,30]
# Print second list
print("list2 items:\n",list2)
# Finding common elements using list comprehension
list3 = [element for element in list1 if element in list2]
print("Common Elements in list1 and list2: ",list3)

Output:

list1 items:
[10, 20, 30, 40, 50]
list2 items:
[10, 15, 20, 25, 30]
Common Elements in list1 and list2: [10, 20, 30]
 

Find Common Elements from List using set intersection method

This example use intersection() method of set to find common elements. It returns a set that contains all the found elements. We can use it to get a set of common elements from two lists.

# Take a list
list1 = [10,20,30,40,50]
# Print first list
print("list1 items:\n",list1)
# Take another list
list2 = [10,15,20,25,30]
# Print second list
print("list2 items:\n",list2)
# Finding common elements using set intersection method
list3 = set(list1).intersection(list2)
print("Common Elements in list1 and list2: ",list3)
print("Type of result:",type(list3))

Output:

list1 items:
[10, 20, 30, 40, 50]
list2 items:
[10, 15, 20, 25, 30]
Common Elements in list1 and list2:  {10, 20, 30}
Type of result: <class 'set'>
 

Sorting List elements in Python

If we want the result to be sorted, then use sorted() method which will return data in natural order. It is useful if we don't want to get data in the previous order.

# Take a list
list1 = [10,20,30,40,50]
# Print first list
print("list1 items:\n",list1)
# Take another list
list2 = [10,15,20,25,30]
# Print second list
print("list2 items:\n",list2)
# Finding common elements using set intersection method
list3 = set(list1).intersection(list2)
# Sorting result
list3 = sorted(list3)
print("Sorted common Elements in list1 and list2: ",list3)

Output:

list1 items:
[10, 20, 30, 40, 50]
list2 items:
[10, 15, 20, 25, 30]
Sorted common Elements in list1 and list2:  [10, 20, 30]
 

Python Example: using filter and lambda operator

If we want to use a functional way to find common elements then we can use filter() function and lambda operator that return a list of elements.

# Take a list
list1 = [10,20,30,40,50]
# Print first list
print("list1 items:\n",list1)
# Take another list
list2 = [10,15,20,25,30]
# Print second list
print("list2 items:\n",list2)
# Finding common elements using set intersection method
list3 = list(filter(lambda element:element in list1, list2))
print("Common Elements in list1 and list2: ",list3)

Output:

list1 items:
[10, 20, 30, 40, 50]
list2 items:
[10, 15, 20, 25, 30]
Common Elements in list1 and list2:  [10, 20, 30]
 


Conclusion

Well, in this topic, we learnt to find common or match elements present in two python lists. We used several ways like: list comprehension, lambda operator, intersection method of set etc. These are some ways by which we can get our task done, hope you get the valuable information.