Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Check if any list element is present in Tuple

Python – Check if any list element is present in Tuple

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))


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




# 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))


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)


Output

['true']

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)


Output

['true']

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"])


Output

['true']

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))


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




# 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.


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




# 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


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




# 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))


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)

RELATED ARTICLES

Most Popular

Recent Comments