Open In App

Python – Check if any list element is present in Tuple

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)



Similar Reads

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() any function is used to perform this task. It just t
4 min read
Python | Check if element is present in tuple
Sometimes, while working with data, we can have a problem in which we need to check if the data we are working with has a particular element. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this task. In this, we iterate through the tuple and check each element if it's ou
7 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list. Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12
11 min read
Python | Check if tuple has any None value
Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values i.e has any None value. This kind of problem is common in data preprocessing steps. Let's discuss certain ways in which this task can be performed. Method #1 : Using any() + map() + lambda Combination of above f
5 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read