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.Constraints:NAExample:Input :1 2 3 4 55 6 7 8 9Output :True
Mention two types of lists and write simple codes to differentiate themAnswer text
Which data structure does not allow duplicate values? List Tuple Dictionary Set
A programmer must know the underlying implementation of the list ADT in order to use a list.TrueFalse
A list can have any number of items and they may have different types of data.Group of answer choicesTrueFalse
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.