Python – Check if any list element is present in Tuple


When it is required to check if any list element is present in a tuple or not, a Boolean value and a simple iteration are used.

Below is a demonstration of the same −

Example

 Live Demo

my_tuple = (14, 35, 27, 99, 23, 89,11)

print("The tuple is :")
print(my_tuple)

my_list = [16, 27, 88, 99]

print("The list is :")
print(my_list)

my_result = False

for element in my_list:
   if element in my_tuple :
      my_result = True
      break

print("The result is :")
if(my_result == True):
print("The element is present in the tuple")
else:
print("The element isn't present in the tuple")

Output

The tuple is :
(14, 35, 27, 99, 23, 89, 11)
The list is :
[16, 27, 88, 99]
The result is :
The element is present in the tuple

Explanation

  • A tuple of integers is defined and is displayed on the console.

  • A list of integers is defined and is displayed on the console.

  • A Boolean value is assigned to ‘False’ initially.

  • The list is iterated over, and if the element in the tuple is present in the list, the Boolean value is reinitialized to ‘True’.

  • The control breaks out of the loop.

  • Depending on the value of Boolean variable, the output is displayed on the console.

Updated on: 04-Sep-2021

554 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements