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 IntersectionSet 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)) OutputTrue 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 ContentUsing any()Using filter() Using a LoopUsing 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) OutputTrue 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)) OutputTrue 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 LoopThis 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) OutputTrue 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. Comment More infoAdvertise with us Next Article Python | Check if two lists have any element in common E everythingispossible Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-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 finding 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. Pythona = [1, 2, 3, 4, 5] b = [4, 5 3 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 listsUsing Set Intersection (Most Efficient)The 3 min read Like