Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Check if element is present in tuple of tuples

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 tests one by one if the element is present as the tuple element. If the element is present, true is returned else false is returned. 

Python3




# Python3 code to demonstrate
# test for values in tuple of tuple
# using any()
 
# initializing tuple of tuple
test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
 
# printing tuple
print("The original tuple is " + str(test_tuple))
 
# using any()
# to test for value in tuple of tuple
if (any('neveropen' in i for i in test_tuple)):
    print("neveropen is present")
else:
    print("neveropen is not present")


Output

The original tuple is (('neveropen', 'gfg'), ('CS_Portal', 'best'))
neveropen is present

Time complexity: The time complexity of this program is O(nm) where n is the length of the outer tuple and m is the length of the inner tuple. 

Auxiliary space: The program uses a constant amount of auxiliary space to store the test tuple and the string to search for.

Method #2: Using itertools.chain() The chain function tests for all the intermediate tuples for the desired values and then returns true if the required value is present in any of the tuples searched. 

Python3




# Python3 code to demonstrate
# test for values in tuple of tuple
# using itertools.chain()
import itertools
 
# initializing tuple of tuple
test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
 
# printing tuple
print("The original tuple is " + str(test_tuple))
 
# using itertools.chain()
# to test for value in tuple of tuple
if ('neveropen' in itertools.chain(*test_tuple)):
    print("neveropen is present")
else:
    print("neveropen is not present")


Output

The original tuple is (('neveropen', 'gfg'), ('CS_Portal', 'best'))
neveropen is present

The time complexity of the code is O(n), where n is the total number of elements in all the tuples.

The auxiliary space complexity of the code is O(1), because we are not using any extra space to store data. 

Method #3: Using list comprehension

Python3




test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
ele="neveropen"
x=[ele for i in test_tuple if ele in i]
print(["yes" if x else "no"])


Output

['yes']

Time Complexity: O(n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

Method #4: Using enumerate function

Python3




test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
ele="neveropen"
x=[ele for a,i in enumerate(test_tuple) if ele in i]
print(["yes" if x else "no"])


Output

['yes']

The time complexity is O(n), where n is the length of the input tuple test_tuple.

The auxiliary space complexity of the code is O(1).

Method #5: Using lambda function

Python3




test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
ele="neveropen"
x=list(filter(lambda i: (i in test_tuple),test_tuple))
print(["yes" if x else "no"])


Output

['yes']

Method: Using “in” operator 

Python3




test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
ele="neveropen" ;a=[]
for i in test_tuple:
  if ele in i:
    a.append(ele)
print(["yes" if a else "no"])


Output

['yes']

Method: Using countOf function 

Python3




import operator as op
test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
ele="neveropen" 
x=[j for i in test_tuple for j in i]
print("yes" if op.countOf(x,ele)>0 else "no")


Output

yes

Method : Using map and lambda:

Approach is to use a combination of the map function and the lambda to check if an element is present in a tuple of tuples. This can be done as follows:

Python3




# Initialize the tuple of tuples
test_tuple = (("neveropen", "gfg"), ("CS_Portal", "best"))
 
# Check if the element is present in any of the tuples
if any(map(lambda t: "neveropen" in t, test_tuple)):
    print("neveropen is present")
else:
    print("neveropen is not present")
#This code is contributed by Edula Vinay Kumar Reddy


Output

neveropen is present

The map function applies a function to each element of an iterable and returns a new iterable with the results. In this case, the lambda function checks if the element is present in the tuple using the in operator, and the any function returns True if any of the elements in the resulting iterable is True.

This approach has a time complexity of O(n), where n is the number of tuples in the input tuple of tuples. It has a space complexity of O(1), since it only uses a constant amount of additional space.

RELATED ARTICLES

Most Popular

Recent Comments