Which operator in python is used to check the existence of tuple item in a tuple?

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.

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

Advertisements

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
 

How do you check if a value exists in a tuple Python?

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.

Which operator is used to check the existence of any particular element in a list tuple?

Checking the existence of an element using in operator In python, the in operator can be used to check the presence of a value in a sequence(string, list, tuple, set, dictionary). It returns a boolean value 'True' if the item is found in the sequence else it returns False.

How do you find the tuple element in Python?

Find an element in tuple by value using 'in' & 'not in'.
# Check if element 34 exists in tuple. ... .
# Check if element 1001 doesn't exists in tuple. ... .
# Find the index of 34 in tuple, if not found then handle the exception. ... .
# Find the index of 24 in tuple, if not found then handle the exception..

Which operator are used in tuple?

Tuple Operations The + operator returns a tuple containing all the elements of the first and the second tuple object. The * operator Concatenates multiple copies of the same tuple. The [] operator Returns the item at the given index. A negative index counts the position from the right side.

Chủ đề