• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Intersection of Lists in Python

Intersection of Lists in Python

Author: Aditya Raj
Last Updated: June 30, 2023

Lists are one of the most used data structures in Python. Sometimes, we might need to find the common elements between any two given lists. In this article, we will discuss how we can perform the intersection operation on two lists in Python to find the common elements between them.

Table of Contents
  1. How to Perform List Intersection in Python?
  2. List Intersection in Python Using a For Loop
  3. Intersection of Lists in Python Using Sets
  4. Conclusion

How to Perform List Intersection in Python?

To perform the intersection of two lists in Python, we just have to create an output list that should contain elements that are present in both the input lists. For instance, if we have list1=[1,2,3,4,5,6] and list2=[2,4,6,8,10,12], the intersection of list1 and list2 will be [2,4,6]. You can observe that each element of the output list belongs to both list1 and list2. In other words, The output list contains only those elements that are present in both the input lists. 

We can perform the intersection of lists in Python using various approaches. Let us discuss them one by one.

List Intersection in Python Using a For Loop

To perform the intersection of two lists, we will first create an empty list named newList to store the elements of the output list. After that, we will traverse the first input list and check if its elements are present in the second input list or not. For this, we will use the membership operator (in operator). If an element is present in both lists, we will append the element to the newList. After execution of the for loop, we will get all the elements that are present in both the input lists in the newList.

list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
newList = []
for element in list1:
    if element in list2:
        newList.append(element)
print("Intersection of the lists is:", newList)

Output:

First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]

Alternatively, you can traverse the second input list and check if its elements are present in the first input list or not. Then, you can add elements to the newList based on the presence of the elements as follows.

list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
newList = []
for element in list2:
    if element in list1:
        newList.append(element)
print("Intersection of the lists is:", newList)

Output:

First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]

I would suggest you traverse the smaller list using the for loop. This will make your code more efficient.

Intersection of Lists in Python Using Sets

Operations like union and intersection have been originally defined for sets. We can also use sets to find the intersection of two lists. To perform the intersection operation on lists by using sets, you can convert the input lists to sets using the set() function. The set() function takes a list as its input and returns a set containing unique elements in the list.

After creating the sets, you can perform the set intersection operation using the intersection() method. The intersection() method, when invoked on a set, takes another set as its input argument. After execution, it returns a set containing the common elements in both sets.

Once we get the intersection set, we can convert the output set back into a list as follows.

list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
set1 = set(list1)
set2 = set(list2)
newList = list(set1.intersection(set2))
print("Intersection of the lists is:", newList)

Output:

First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]

Conclusion

In this article, we have discussed how we can perform the intersection of lists in Python. To know more about lists, you can read this article on list comprehension. You might also like this article on how to reverse a list in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us