Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Check if any list element is present in Tuple
Next article icon

Python | Check if two lists have any element in common

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check.

Using set Intersection

Set intersection uses Python’s built-in set data structure to efficiently identify common elements between two lists. The intersection operation directly returns a set containing elements that are present in both lists.

Python
a = [1, 2, 3, 4] b = [3, 5, 6]  # Check for common elements common = set(a) & set(b)  print(bool(common))   

Output
True 

Explanation:

  • Converts both lists to sets which allowing fast membership testing.
  • Performs the & (intersection) operation to find common elements.
  • Returns True if any elements overlap.

Let’s explore some more methods and see how we can check if two lists have any element in common.

Table of Content

  • Using any()
  • Using filter()
  • Using a Loop

Using any()

This method combines a generator expression with Python’s built-in any() to streamline the process. It stops as soon as it finds the first match, making it more efficient than looping through all elements.

Python
a = [1, 2, 3, 4] b = [3, 5, 6]  # Check for common elements using any() common = any(x in a for x in b)  print(common) 

Output
True 

Explanation:

  • The generator expression checks if any element from the first list exists in the second list.
  • any() returns True if at least one common element is found.

Using filter()

filter() function can also be employed by applying a condition that checks if an element from one list exists in another list. It works by iterating over the first list and including only the elements that satisfy the condition (i.e., they are also present in the second list).

Python
a = [1, 2, 3, 4] b = [3, 5, 6]  # Check for common elements using filter() common = list(filter(lambda x: x in b, a))  print(bool(common)) 

Output
True 

Explanation:

  • It Filters the elements of 'a' based on their presence in 'b'.
  • Converts the filter object to a list to determine if there are common elements.

Using a Loop

This method uses a loop and involves iterating through one list and checking if each element exists in the second list. This method typically uses the in keyword inside the loop to verify membership.

Python
a = [1, 2, 3, 4] b = [3, 5, 6]  # Check for common elements using a loop common = False for x in a:     if x in b:         common = True         break  print(common) 

Output
True 

Explanation:

  • Here, the loop iterates through the first list and checks if any element exists in the second list.
  • It Stops immediately when a common element is found.


Next Article
Python - Check if any list element is present in Tuple
author
everythingispossible
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Python - Check if two lists have at-least one element common
    Checking if two lists share at least one common element is a frequent task when working with datasets, filtering, or validating data. Python offers multiple efficient ways to solve this depending on the size and structure of the lists. Using set IntersectionConverting both lists into sets and findin
    3 min read
  • Check if any element in list satisfies a condition-Python
    The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
    3 min read
  • Python - Check if any list element is present in Tuple
    Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements.
    6 min read
  • Python Check if the List Contains Elements of another List
    The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False. Using
    3 min read
  • Remove common elements from two list in Python
    When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
    3 min read
  • How to Check if an Index Exists in Python Lists
    When working with lists in Python, sometimes we need to check if a particular index exists. This is important because if we try to access an index that is out of range, we will get an error. Let's look at some simple ways to check if an index exists in a Python list. The easiest methods to check if
    2 min read
  • Python - Print all common elements of two lists
    Finding common elements between two lists is a common operation in Python, especially in data comparison tasks. Python provides multiple ways to achieve this, from basic loops to set operations. Let's see how we can print all the common elements of two lists Using Set Intersection (Most Efficient)Th
    3 min read
  • Python - Check if all elements in List are same
    To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
    3 min read
  • Python - Check if elements index are equal for list elements
    Given two lists and check list, test if for each element in check list, elements occur in similar index in 2 lists. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7] Output : False Explanation : 7 is at 4th and 2nd place in both list, hence False. Input : tes
    4 min read
  • Python | Check if element exists in list of lists
    Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. C/C++ Code # Python code to dem
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences