Q:

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

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to remove given character from the ... >>
<< Python program to assign frequency to tuples...