Python program to check if any list element is present in Tuple

Here, we have a list and a tuple and we need to check if there exists any one element which is common in both list and tuple in Python.
Submitted by Shivang Yadav, on August 24, 2021

We have two different collections in Python that are list and tuple. And we check if there's anyone element that is common for both collections list and tuple. And return true and false based on the presence.

Before proceeding further, let's recap some topics that we will need for solving the problem.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

```tuple = ("python", "includehelp", 43, 54.23)
```

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []

Example:

```list = [3 ,1,  5, 7]
```

That is all the basic information required here. Now, let's get back to the problem and see the sample inputs and their outputs for a better understanding of the problem.

```Input:
myTup = (5, 1, 8, 3, 9) myList = [2, 4, 7, 8, 0]

Output:
true
```

Now, let's see methods to solve the problem. To do this, we need to check if there is any element that is present in both the arrays.

For this, we will traverse both and find any common element, if present and return a boolean value based on it.

Python programming language allows us to solve this problem in multiple ways and also some built-in methods from the python library can be helpful in performing the task.

Method 1:

A method that can be used to solve the problem is by traversing all the structures and checking if there exists one element which is present in both and returns a boolean value based on it.

Program to check if any list element is present in Tuple

```# Python program to check if any list element
# is present in tuple

# Creating and printing lists and tuples
myTuple = (5, 1, 8, 3, 9)
print("The tuple elements are " + str(myTuple))
myList = [2, 4, 7, 8, 0]
print("The list elements are " + str(myList))

# Checking if any list element
# is present in tuple
isPresent = False
for ele in myList:
if ele in myTuple :
isPresent = True
break

print("Is there any element in the list which is also present in tuple ? : " + str(isPresent))
```

Output:

```The tuple elements are (5, 1, 8, 3, 9)
The list elements are [2, 4, 7, 8, 0]
Is there any element in the list which is also present in tuple ? : True
```

Method 2:

Another method that can be utilized to check if any element is present in the tuple is by using any() method along with comprehension. This will return true if any element is common in both else return false.

```# Python program to check if any list element
# is present in tuple

# Creating and printing lists and tuples
myTuple = (5, 1, 8, 3, 9)
print("The tuple elements are " + str(myTuple))
myList = [2, 4, 7, 8, 0]
print("The list elements are " + str(myList))

# Checking if any list element is present in tuple
isPresent = any(val in myTuple for val in myTuple)

print("Is there any element in the list which is also present in tuple ? : " + str(isPresent))
```

Output:

```The tuple elements are (5, 1, 8, 3, 9)
The list elements are [2, 4, 7, 8, 0]
Is there any element in the list which is also present in tuple ? : True
```

Preparation

What's New

Top Interview Coding Problems/Challenges!