Python

List Intersection in Python

Many object variables exist in python to store a variety of data types. The list is one of these variables and can store different types of data for different needs. Sometimes, we need to find common, uncommon, or both common and uncommon data items from the multiple lists for programming purposes. Python contains several built-in functions and operators that can perform these types of tasks for Python sets. Finding common data from the multiple lists is called list intersection, but there is no operator or built-in function for lists like sets to find the common data items from multiple lists. This tutorial will show you how to intersect lists in Python.

Intersection

Before beginning this tutorial, the concept of intersection will be explained. Below, two are given as an example:

List1 = [4, 7, 23, 9, 6, 11]
List2 = [6, 5, 1, 20, 9, 7, 4, 3]

After applying the intersection, if the result is stored in another list, perhaps named List3, then it will contain the following data.

List3 = [4, 6, 7, 9]

The graphical representation of List3 is shown below:

Example 1: Intersecting Lists via the set() Method

The following example shows how you can intersect two lists using the set() method and the & operator. Two lists, named list1 and list2, are declared here. Both of these lists contain numeric values. The third list is generated by converting list1 and list 2 into the set via the set() method and applying intersection by using the & operator. Next, the values of the three lists are printed.

#!/usr/bin/env python3
# Define list1
list1 = [22, 8, 45, 11, 34, 9, 20, 7]
# Define list2
list2 = [12, 9, 20, 78, 53, 8, 2, 30,31]
# Create list3 by intersecting list1 and list2
list3 = list(set(list1) & set(list2))
# Print list1
print("The values of list1:\n",list1)
# Print list2
print("The values of list2:\n",list2)
# Print list3
print("The values of list3 after intersecting list1 and list2:\n",list3)

Output

The following output will appear after running the script. Three values are common to both lists. These values are 8, 9, and 20, and are shown in the content of the third list.

Example 2: Intersecting Lists via the intersection() Method

There is no direct, built-in method for the list object to find out the intersection values between two lists. However, the set has a built-in method, named intersection(), to find out the common values between the sets. A custom function, named intersectionLists(), is defined in the following script to find out the common values between the two lists. One limitation of this set is that it can contain only numerical data. A list variable can contain various types of data string, including numerical, boolean, etc. In the script, list1 and list2 contain all string data including and text. The script will print all common string and numeric values between list1 and list2.

#!/usr/bin/env python3
 
# Define the function to return the intersection of two lists
def intersectLists(list1,list2):
  return set(list1).intersection(list2)
 
# Define list1
list1 = ['Hello','7','8','10','2']
# Define list2
list2 = ['8','34','Hello','2','21','1',10]
''' Call the custom function to store
  the intersection result of list1 and list2
  into list3'''

list3 = intersectLists(list1, list2)
# Print list1
print("The values of list1:\n",list1)
# Print list2
print("The values of list2:\n",list2)
# Print list3
print("The values of list3 after intersecting list1 and list2:\n",list3)

Output

The following output will appear after running the script. Here, common values between the two lists are ‘2’, ‘8’, and ‘Hello.’

Example 3: Intersecting Lists via the loop Method

The previous two examples show list intersection using built-in functions. The following example shows list intersection without any built-in function. This can be achieved by using the loop. Two lists of text data are declared in the script. The for loop is used here to find out the common text values between list1 and list2, and that are stored in the variable, list3. Next, the values of these three list variables are printed.

#!/usr/bin/env python3
 
# Define list1
list1 = ['Farhan','Abir','Akash','Meena','Mazher']
# Define list2
list2 = ['Meena','Moyna','Nobel','Naher','Abir','Farheen']
# Find the intersection using for loop and store the result into list3
list3 = [value for value in list1 if value in list2]
 
# Print list1
print("The values of list1:\n",list1)
# Print list2
print("The values of list2:\n",list2)
# Print list3
print("The values of list3 after intersecting list1 and list2:\n",list3)

Output

The following output will appear after running the script. Here, the common text values of the two lists are ‘Abir’ and ‘Meena.’

Example 4: Intersecting Simple And Nested Lists via the filter() Method

The following example shows the intersection between a simple list and a nested list. The filter() method and lambda function are used in the script to conduct the intersection between  list1 and list2. When a list contains one or more lists as list items, then the list is called a nested list. Here, list1 is a simple list, and list2 is a nested list, while list3 contains the intersection values of list1 and list2.

#!/usr/bin/env python3
 
# Define a simple list
list1 = [22, 8, 45, 11, 34, 9, 20, 7]
# Define a nested list
list2 = [[12, 9, 20], [78, 11, 53, 8], [2, 30, 31, 45]]
# Create list3 by intersecting list1 and list2 using filter()
list3 = [list(filter(lambda n: n in list1, slist)) for slist in list2]
 
# Print list1
print("The values of list1:\n",list1)
# Print list2
print("The values of list2:\n",list2)
# Print list3
print("The values of list3 after intersecting list1 and list2:\n",list3)

Output

The following output will appear after running the script. The list2 variable contains three other lists as list items. So, the result shows the intersection between these three sub-lists of list2 with list1.

Conclusion

List intersection is a useful way to find the common values between lists that contain a large number of list items. Methods for performing list intersection with and without built-in functions are shown in this tutorial. After reading this article, the concept of the list intersection should be clear, and you should be able to apply list intersection in your script by following any way shown in this tutorial.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.