Write a function to take two lists and print if they have at least one common member(number)?

Basic way to checks if two lists have common elements is using traversal of lists in Python. You can checks single match or all element match between 2 lists.

Python checks if two lists have common elements

Simple example code where given two lists a, b. Check if two lists have at least one element common in them or all elements are same.

Check if two lists have at-least one element common

Using for loop

def common_ele(list1, list2): res = False # traverse in the 1st list for x in list1: # traverse in the 2nd list for y in list2: # if one common if x == y: res = True return res return res a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 5] print(common_ele(a, b))

Using Set Intersection

set.intersection will find any common elements:

def common_ele(list1, list2): a_set = set(a) b_set = set(b) if len(a_set.intersection(b_set)) > 0: return True return False a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 5] print(common_ele(a, b))

Output: True

Check if Python List Contains All Elements of Another List

Use the all() method.

List1 = ['python', 'JS', 'c#', 'go', 'c', 'c++'] List2 = ['c#', 'Java', 'python'] check = all(item in List1 for item in List2) if check: print("Both list same") else: print("No, lists are not same.")

Output:

Write a function to take two lists and print if they have at least one common member(number)?

Do comment if you have any doubts and suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Write a function to take two lists and print if they have at least one common member(number)?

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

I'm a programming semi-noob and am working through Torbjoern Lager's 46 Simple Python Exercises. This is number 10: Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.

def over(list1,list2): for i in list1: for j in list2: return i==j

I thought I had a nice, simple solution, but it can't recognize that the lists overlap, unless the overlapping elements are the first ones.

over(["a","b","c","d"],["e","f","a","h"])

returns False

over(["a","b","c","d"],["a","f","g","h"])

returns True

For some reason, it's not searching through all of the combinations. Any help would be appreciated.

Hits: 135

Write a function to take two lists and print if they have at least one common member(number)?

Write a Python function that takes two lists and returns True if they have at least one common member.

Sample Solution

Python Code:

def common_data(list1, list2): result = False for x in list1: for y in list2: if x == y: result = True return result print(common_data([1,2,3,4,5], [5,6,7,8,9])) print(common_data([1,2,3,4,5], [6,7,8,9]))

Sample Output:

True None

Write a Python function that takes two lists and returns True if they have at least one common member

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

PythonServer Side ProgrammingProgramming

In this problem we use two user input list. Our task is to check that there is any common element or not. We use very simple traversing technique, traverse both the list and check the every element of first list and second list.

Example

Input : A = [10, 20, 30, 50] B = [90, 80, 30, 10, 3] Output : FOUND Input : A = [10, 20, 30, 50] B = [100,200,300,500] Output : NOT FOUND

Algorithm

commonelement(A,B) /* A and B are two user input list */ Step 1: First use one third variable c which is display the result. Step 2: Traverse both the list and compare every element of the first list with every element of the second list. Step 3: If common element is found then c display FOUND otherwise display NOT FOUND.

Example Code

# Python program to check # if two lists have at-least # one element common # using traversal of list def commonelement(A, B): c = "NOT FOUND" # traverse in the 1st list for i in A: # traverse in the 2nd list for j in B: # if one common if i == j: c="FOUND" return c return c # Driver code A=list() B=list() n1=int(input("Enter the size of the first List ::")) print("Enter the Element of first List ::") for i in range(int(n1)): k=int(input("")) A.append(k) n2=int(input("Enter the size of the second List ::")) print("Enter the Element of second List ::") for i in range(int(n2)): k=int(input("")) B.append(k) print("Display Result ::",commonelement(A, B))

Output

Enter the size of the first List ::4 Enter the Element of first List :: 2 1 4 9 Enter the size of the second List ::5 Enter the Element of second List :: 9 90 4 89 67 Display Result :: FOUND Enter the size of the first List ::4 Enter the Element of first List :: 67 89 45 23 Enter the size of the second List ::4 Enter the Element of second List :: 1 2 3 4 Display Result :: NOT FOUND

Write a function to take two lists and print if they have at least one common member(number)?

Published on 26-Sep-2018 13:31:42