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 our, if found we return True.
Python3
# Python3 code to demonstrate working of # Check if element is present in tuple # using loop # initialize tuple test_tup = ( 10 , 4 , 5 , 6 , 8 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # initialize N N = 6 # Check if element is present in tuple # using loop res = False for ele in test_tup: if N = = ele: res = True break # printing result print ( "Does tuple contain required value ? : " + str (res)) |
Output
The original tuple : (10, 4, 5, 6, 8) Does tuple contain required value ? : True
Time complexity: O(n)
Auxiliary space: O(n), where n is length of tuple.
Method #2: Using in operator
It is used to perform this task. It is a one-liner and recommended to perform this task.
Python3
# Python3 code to demonstrate working of # Check if element is present in tuple # Using in operator # initialize tuple test_tup = ( 10 , 4 , 5 , 6 , 8 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # initialize N N = 6 # Check if element is present in tuple # Using in operator res = N in test_tup # printing result print ( "Does tuple contain required value ? : " + str (res)) |
Output
The original tuple : (10, 4, 5, 6, 8) Does tuple contain required value ? : True
Method 3: Using list comprehension method
Python3
t = ( 10 , 4 , 5 , 6 , 8 ) n = 6 x = [i for i in t if i = = n] print ([ "yes" if x else "no" ]) |
Output
['yes']
Method 4: Using lambda function
Python3
t = ( 10 , 4 , 5 , 6 , 8 ) n = 6 x = tuple ( filter ( lambda i: (i = = n), t)) print ([ "yes" if x else "no" ]) |
Output
['yes']
Method 5: Using the enumerate function
Python3
t = ( '10' , '4' , '5' , '6' , '8' ) n = 6 x = [ int (i) for i in t if int (i) = = n] print ([ "yes" if x else "no" ]) |
Output
['yes']
Method 6 : Using count() method
Python3
# Python3 code to demonstrate working of # Check if element is present in tuple # using loop # initialize tuple test_tup = ( 10 , 4 , 5 , 6 , 8 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # initialize N N = 6 # Check if element is present in tuple # using loop res = False if (test_tup.count(N) > = 1 ): res = True # printing result print ( "Does tuple contain required value ? : " + str (res)) |
Output
The original tuple : (10, 4, 5, 6, 8) Does tuple contain required value ? : True
Method 7: Using try/except and index:
Python3
# Python3 code to demonstrate working of # Check if element is present in tuple # using loop # initialize tuple test_tup = ( 10 , 4 , 5 , 6 , 8 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # initialize N N = 6 # Check if element is present in tuple # using loop res = False try : test_tup.index(N) res = True except ValueError: pass # printing result print ( "Does tuple contain required value ? : " + str (res)) #this code is contributed by edula vinay kumar reddy |
Output
The original tuple : (10, 4, 5, 6, 8) Does tuple contain required value ? : True
Time complexity: O(n)
Auxiliary Space : O(1)
Method 8: Using recursion method:
Python3
# Python3 code to demonstrate working of # Check if element is present in tuple # using recursion method #defining recursion function def value_present(start,lst,value): if start = = len (lst): #base condition return False if lst[start] = = value: #checking lst[start] is value or not return True return value_present(start + 1 ,lst,value) #recursive call # initialize tuple test_tup = ( 10 , 4 , 5 , 6 , 8 ) # printing original tuple print ( "The original tuple : " + str (test_tup)) # initialize N N = 6 res = value_present( 0 ,test_tup,N) print ( "Does tuple contain required value ? : " + str (res)) #this code is contributed by tvsk |
Output
The original tuple : (10, 4, 5, 6, 8) Does tuple contain required value ? : True
Time complexity: O(n)
Auxiliary Space : O(n)
Please Login to comment...