WAP that takes two lists and return true if they have atleast one common member.
Question
WAP that takes two lists and return true if they have atleast one common member.
Solution
Sure, here is a Python program that takes two lists as input and returns True if they have at least one common member.
def common_member(list1, list2):
# Use set() to remove duplicates from lists
set1 = set(list1)
set2 = set(list2)
# Use & operator to find common elements in sets
if set1 & set2:
return True
else:
return False
# Test the function
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
print(common_member(list1, list2)) # Output: True
list3 = [1, 2, 3]
list4 = [4, 5, 6]
print(common_member(list3, list4)) # Output: False
In this program, the function common_member takes two lists as arguments. It converts the lists to sets using the set() function, which removes any duplicate elements. Then it uses the & operator to find the intersection of the two sets, i.e., the common elements. If there are any common elements, it returns True; otherwise, it returns False.
Similar Questions
Common elementGiven two lists a, b. Check if two lists have at least one element common in them.
Common elementGiven two lists a, b. Check if two lists have at least one element common in them.Constraints:NAExample:Input :1 2 3 4 55 6 7 8 9Output :TrueExplanation:5 is common is 2 lists
Common elementGiven two lists a, b. Check if two lists have at least one element common in them.Constraints:NAExample:Input :1 2 3 4 55 6 7 8 9Output :True
Compare two linked lists
16. a = [1,2,3]b= aa == ba is bc = list(a)a == ca is cb == cWhich of the following statements is True?a is b: This statement checks if a and b reference the same object in memory, and it returns True.a == c: This statement checks if a and c contain the same elements, and it returns False because they are different objects.b == c: This statement checks if b and c contain the same elements, and it returns False because they reference different objects.a is c: This statement checks if a and c reference the same object in memory, and it returns True because they were created from the same list.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.