In this article we will discuss different ways to check if an element exists in tuple or not. If yes then also find its index and occurrence count.

Find an element in tuple by value using ‘in’ & ‘not in’

Python provides operator in and not in to check if an element exists in tuple.

Suppose we have a tuple,

# A tuple of numbers    
tupleObj = (12 , 34, 45, 22, 33 , 67, 34, 56 )

Let’s check if element with value 34 exists in tuple using in operator,
# Check if element 34 exists in tuple
if 34 in tupleObj:
    print("Element Found in Tuple")
else:
    print("Element not Found in Tuple")    

As 34 exists in tuple, so output will be,
Element Found in Tuple

Now let’s check if an element with value 1001 does not exists in tuple using not in operator,
# Check if element 1001 doesn't exists in tuple    
if 1001 not in tupleObj:
    print("Yes, element Not In tuple")
else:
    print("Element is in Tuple")    

As 1001 does not exists in tuple, so output will be,
Yes, element Not In tuple

Find the index of an element in tuple using index()

Sometimes just checking if an element exists in tuple is not sufficient, we want to find it’s position of first occurrence in tuple.┬áTuple provides a member function index() i.e.

tuple.index(x)

It returns the index for first occurrence of x in the tuple. Also, if element is not found in tuple then it will throw an exception ValueError.
Check out these examples,
Example 1:
# Find the index of 34 in tuple, if not found then handle the exception
try :
    pos = tupleObj.index(34)
    print("Element 34 Found at : " , pos)
except ValueError as e:
    print(e)    

As 34 exists in the tuple therefore output will be,
Element 34 Found at :  1

Example 2:
# Find the index of 24 in tuple, if not found then handle the exception
try :
    pos = tupleObj.index(24)
    print("Element 24 Found at : " , pos)
except ValueError as e:
    print(e)

As 24 does not exists in the tuple therefore output will be,
tuple.index(x): x not in tuple

Find the occurrence count of an element in the tuple using count()

Tuple provides an another member function count() i.e.

tuple.count(elem)

It returns the number of times elem appears in tuple.

Let’s find the occurrence count if element 34 in tuple i.e.

# Get the count of how many times 34 appears in tuple
count = tupleObj.count(34)

print("Count of 34 in tuple is : ", count)

As 34 exists in tuple multiple times, so output will be,
Count of 34 in tuple is :  2

Based on occurrence count we can also check if element exists in tuple i.e.
if tupleObj.count(34) > 0 :
    print("34 Found in Tuple")
else:
    print("34 Not Found in Tuple")

Output:
34 Found in Tuple

Complete example is as follows,
def main():
    
    # A tuple of numbers    
    tupleObj = (12 , 34, 45, 22, 33 , 67, 34, 56 )
    
    print("**** Find an element in tuple using 'in' & 'not in' *****")
    
    # Check if element 34 exists in tuple
    if 34 in tupleObj:
        print("Element Found in Tuple")
    else:
        print("Element not Found in Tuple")    
    
    # Check if element 1001 doesn't exists in tuple    
    if 1001 not in tupleObj:
        print("Yes, element Not In tuple")
    else:
        print("Element is in Tuple")    

    
    print("**** Find the index of an element in Tuple *****")
    
    # Find the index of 24 in tuple, if not found then handle the exception
    try :
        pos = tupleObj.index(24)
        print("Element 24 Found at : " , pos)
    except ValueError as e:
        print(e)
    
    # Find the index of 34 in tuple, if not found then handle the exception
    try :
        pos = tupleObj.index(34)
        print("Element 34 Found at : " , pos)
    except ValueError as e:
        print(e)    
    
    print("**** Find the occurence count an element in the Tuple *****")    
    
    # Get the count of how many times 34 appears in tuple
    count = tupleObj.count(34)
    
    print("Count of 34 in tuple is : ", count)
   
    # Based on occurrence count check if element exists in tuple
    if tupleObj.count(34) > 0 :
        print("34 Found in Tuple")
    else:
        print("34 Not Found in Tuple")    
        
         
           
if __name__ == '__main__':
    main()

Output:
**** Find an element in tuple using 'in' & 'not in' *****
Element Found in Tuple
Yes, element Not In tuple
**** Find the index of an element in Tuple *****
tuple.index(x): x not in tuple
Element 34 Found at :  1
**** Find the occurence count an element in the Tuple *****
Count of 34 in tuple is :  2
34 Found in Tuple

 

Join a list of 2000+ Programmers for latest Tips & Tutorials