Python - Check if any list element is present in Tuple
Last Updated :
02 May, 2023
Given a tuple, check if any list element is present in it.
Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11]
Output : True
Explanation : 10 occurs in both tuple and list.
Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11]
Output : False
Explanation : No common elements.
Method #1: Using loop
In this, we keep a boolean variable, keeping record of all elements, if found, then returns True, else False.
Python3
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
# Using loop
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
res = False
for ele in check_list:
# checking using in operator
if ele in test_tup :
res = True
break
# printing result
print("Is any list element present in tuple ? : " + str(res))
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1) additional space is not required
Method #2: Using any()
This returns True, if any element of list is found in tuple, test using in operator.
Python3
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
# Using any()
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
# generator expression is used for iteration
res = any(ele in test_tup for ele in check_list)
# printing result
print("Is any list element present in tuple ? : " + str(res))
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Method #3: Using list comprehension
Python3
test_tup = (4, 5, 7, 9, 3)
check_list = [6, 7, 10, 11]
x=["true" for i in check_list if i in test_tup]
print(x)
Time complexity: O(n)
Auxiliary space: O(1)
Method #4: Using enumerate function
Python3
test_tup = (4, 5, 7, 9, 3)
check_list = [6, 7, 10, 11]
x=["true" for a,i in enumerate(check_list) if i in test_tup]
print(x)
Method #5: Using lambda function
Python3
test_tup = (4, 5, 7, 9, 3)
check_list = [6, 7, 10, 11]
x=list(filter(lambda i:(i in check_list),test_tup))
print(["true" if x else "false"])
Method #6: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
import operator as op
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
res = False
for ele in check_list:
# checking using in operator
if op.countOf(test_tup, ele) > 0:
res = True
break
# printing result
print("Is any list element present in tuple ? : " + str(res))
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method #7: Using any() + map() function
Python3
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
# Using any() + map() function
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
# using any() + map() function
res = any(map(lambda x: x in test_tup, check_list))
# printing result
print("Is any list element present in tuple ? : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method#8: Using Recursive method.
Python3
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
def is_element_present(test_tup, check_list):
if not check_list:
return False
if check_list[0] in test_tup:
return True
return is_element_present(test_tup, check_list[1:])
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
# printing result
print("Is any list element present in tuple ? : " + str(is_element_present(test_tup, check_list)))
#this code contributed by tvsk
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Time Complexity: O(n)
Space Complexity: O(n)
Method 11: Using while loop and in operator:
Approach:
- Initialize the test_list.
- Initialize the check_list.
- Initialize the boolean variable that contains result.
- Initialize 'i' variable that contains the length of the list.
- While loop iterate 'i' times.
- Inside while loop checks the condition element at the index 'i' present in the tuple or not.
- If the element is present in the tuple break the loop and print True for the result.
- Else print False for the result.
Python
# Python3 code to demonstrate working of
# Check if any list element is present in Tuple
# Using loop
# initializing tuple
test_tup = (4, 5, 7, 9, 3)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# initializing list
check_list = [6, 7, 10, 11]
# Variable to store result and length of list
res = False
i = len(check_list) - 1
#Checking presence of element
while i >= 0:
# checking using in operator
if check_list[i] in test_tup :
res = True
break
i = i - 1
# printing result
print("Is any list element present in tuple ? : " + str(res))
OutputThe original tuple is : (4, 5, 7, 9, 3)
Is any list element present in tuple ? : True
Time Complexity: O(N), where N is the length of the list
Auxiliary Space: O(N)
Similar Reads
Python - Check if element is present in tuple We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read
Python | Check if element is present in tuple of tuples Sometimes the data that we use is in the form of tuples and often we need to look into the nested tuples as well. The common problem that this can solve is looking for missing data or na value in data preprocessing. Let's discuss certain ways in which this can be performed. Method #1: Using any() an
4 min read
Test if all elements are present in list-Python The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.U
3 min read
Python | Check if any String is empty in list Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let's discuss if a list is perfect on this factor using certain methods. Method #1 : Using any() + len() The combinati
6 min read
Python | Filter tuples according to list element presence Sometimes, while working with records, we can have a problem in which we have to filter all the tuples from a list of tuples, which contains atleast one element from a list. This can have applications in many domains working with data. Let's discuss certain ways in which this task can be performed.
8 min read