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
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.