Given two lists a, b. Check if two lists have at least one element common in them.
Input : a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] Output : True Input : a=[1, 2, 3, 4, 5] b=[6, 7, 8, 9] Output : False
Method 1 : Traversal of List
Using traversal in two lists, we can check if there exists one common element at least in them. While traversing two lists if we find one element to be common in them, then we return true. After complete traversal and checking, if no elements are same, then we return false.
Method 2 : Using Set and Property
Using set’s and property, if there exists at least one common element then set(a)&set(b) returns a positive integer, if it does not contains any positive integer, then it returns 0. So we insert a in set_a and b in set_b and then check if set_a & set_b for a positive integer or not.
Method 3 : Using Set Intersection
Using set’s intersection inbuilt function. a_set.intersection(b_set) returns a positive integer if there is at least one element in common, else it returns 0. So we insert a in set_a and b in set_b and then check a_set.intersection(b_set), and returns depending on the value.
- Python | Check if two lists have any element in common
- Python | Print all the common elements of two lists
- Python | Zipping two lists of lists
- Python | Check if k occurs atleast n times in a list
- Python program to check if given value occurs atleast k times
- Filter words from a given Pandas series that contain atleast two vowels
- Python - Check if all tuples have element difference less than K
- Python program to find common elements in three lists using sets
- Python | Find common elements in list of lists
- Python | Merge List with common elements in a List of Lists
- Python | Count of common elements in the lists
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Python | Concatenate two lists element-wise
- Python | Merge two list of lists according to first element
- Python program to check if a string has at least one letter and one number
- Python | Check if element exists in list of lists
- Python | Check whether two lists are circularly identical
- Python | Check if two lists are identical
- Python | Check whether two lists follow same pattern or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.