Knowee
Questions
Features
Study Tools

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

Question

Common element

Given two lists a, b. Check if two lists have at least one element common in them.

Constraints: NA
Example:
Input:
1 2 3 4 5
5 6 7 8 9

Output:
True

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution in Python:

Step 1: Define the two lists.

a = [1, 2, 3, 4, 5]
b = [5, 6, 7, 8, 9]

Step 2: Use the built-in Python function set() to convert the lists into sets. Sets in Python are unordered collections of unique elements. Converting the lists to sets will remove any duplicate elements.

set_a = set(a)
set_b = set(b)

Step 3: Use the intersection() method, which returns a set that contains the similarity between two or more sets. If the length of the intersection is greater than 0, it means that there is at least one common element between the two lists.

common_elements = set_a.intersection(set_b)

Step 4: Check if the length of common_elements is greater than 0. If it is, return True. Otherwise, return False.

if len(common_elements) > 0:
    print(True)
else:
    print(False)

When you run this code with the given lists, it will print True because the number 5 is a common element in both lists.

This problem has been solved

Similar Questions

Symmetric pairsExtract all the pairs which are symmetric, i.e for any (x, y), we have (y, x) pair presentConstraints:NAExample:Input :36 72 37 6Output : (6, 7)

Given Binary Tuple representing  binary representation of number, convert to integer.Constraints:NAExample:Input:1 1 0Output :6

WAP that takes two lists and return true if they have atleast one common member.

If A = {7, 8} and B = {2, 4, 6}, then A × B = {(7, 2), (7, 4), (7, 6), (8, 2), (8, 4), (8, 6)}TrueFalse

If A = {1, 2, 3, 4, 5, 6}, B = {2, 4, 6, 8}.Then A – B is{1, 3, 5}{2, 4, 6, 8}{1, 2, 3, 4, 5, 6}None of these

1/1

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.