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
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
res = False
for ele in check_list:
if ele in test_tup :
res = True
break
print ( "Is any list element present in tuple ? : " + str (res))
|
Output
The 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
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
res = any (ele in test_tup for ele in check_list)
print ( "Is any list element present in tuple ? : " + str (res))
|
Output
The 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
import operator as op
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
res = False
for ele in check_list:
if op.countOf(test_tup, ele) > 0 :
res = True
break
print ( "Is any list element present in tuple ? : " + str (res))
|
Output
The 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
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
res = any ( map ( lambda x: x in test_tup, check_list))
print ( "Is any list element present in tuple ? : " + str (res))
|
Output
The 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
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 :])
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
print ( "Is any list element present in tuple ? : " + str (is_element_present(test_tup, check_list)))
|
Output
The 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
test_tup = ( 4 , 5 , 7 , 9 , 3 )
print ( "The original tuple is : " + str (test_tup))
check_list = [ 6 , 7 , 10 , 11 ]
res = False
i = len (check_list) - 1
while i > = 0 :
if check_list[i] in test_tup :
res = True
break
i = i - 1
print ( "Is any list element present in tuple ? : " + str (res))
|
Output
The 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)
Please Login to comment...