• 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: March 3, 2022

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.

How to perform the intersection of lists 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.

Intersection of two lists 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. 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 two lists in python using a 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. After that, you can perform the set intersection operation using the intersection() method. Finally, you 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.

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 bitly 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 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 Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Select Multiple Columns in a Pandas Dataframe
  • Select Specific Columns in Pandas Dataframe
  • Convert Epoch to Datetime in Python
  • Iterate Rows in a Pandas Dataframe
  • Split String Into Characters in Python

Copyright © 2012–2023 ยท PythonForBeginners.com

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