Tuples are variable types that allow you to store multiple items in a single variable. Tuple is one of Python’s four built-in data types for storing data collections. The other three are List, Set, and Dictionary, each with unique properties and applications. Tuples are collections that are both ordered and immutable.
Given a tuple, the task is to determine whether or not a given element is present in the tuple.
Examples:
Input:
given_tuple =( 3,4,5,6,2) element =2
Output:
The item is found in the tuple
Search an element in Tuple by value
There are several ways to search an element in tuple some of them are:
Method #1:Using for loop
This is a brute force method for completing this task. We iterate through the tuple and check each element to see if it is given element. If the element matches then written true else return false.
Below is the implementation:
# function which returns true if given element is found in tuple def searchElement(given_tuple, element): # using for loop to traverse the tuple for value in given_tuple: # if the given element is equal to the value then return true if(value == element): return True # if the element is not found in tuple then return False return False # given tuple given_tuple = (3, 4, 5, 6, 2) # given element element = 2 # passing tuple and element to searchElement function if(searchElement(given_tuple, element)): print("The item is found in the tuple") else: print("The specified element does not exist in the tuple")
Output:
The item is found in the tuple
Method #2:Using in operator
Using the in operator is the most Pythonic way to accomplish this task. It is a one-liner that is recommended for this task.
Below is the implementation:
# function which returns true if given element is found in tuple def searchElement(given_tuple, element): #using in operator # if the given eleement is present then return true if element in given_tuple: return True else: return False # given tuple given_tuple = (3, 4, 5, 6, 2) # given element element = 2 # passing tuple and element to searchElement function if(searchElement(given_tuple, element)): print("The item is found in the tuple") else: print("The specified element does not exist in the tuple")
Output:
The item is found in the tuple
Method #3:Using not in operator
Another method for searching for an element in a tuple is to use the not in operator.
If the element is not present in the tuple, it returns True otherwise, it returns False.
Below is the implementation:
# function which returns true if given element is found in tuple def searchElement(given_tuple, element): #using in operator # if the given eleement is not present then return false if element not in given_tuple: return False else: return True # given tuple given_tuple = (3, 4, 5, 6, 2) # given element element = 2 # passing tuple and element to searchElement function if(searchElement(given_tuple, element)): print("The item is found in the tuple") else: print("The specified element does not exist in the tuple")
Output:
The item is found in the tuple
Method #4:Using count() function
Tuple has another member function count(),
given_tuple.count(element)
It returns the number of occurrences of element in the tuple.
If the count of an element is greater than zero, the element is present in the tuple .otherwise, the element is not present in the tuple.
Below is the implementation:
# function which returns true if given element is found in tuple def searchElement(given_tuple, element): # using count() function to count the element in tuple element_count = given_tuple.count(element) # if the count is greater than 0 then return True if element_count > 0: return True else: return False # given tuple given_tuple = (3, 4, 5, 6, 2) # given element element = 2 # passing tuple and element to searchElement function if(searchElement(given_tuple, element)): print("The item is found in the tuple") else: print("The specified element does not exist in the tuple")
Output:
The item is found in the tuple
Method #5:Using index function
Sometimes just checking if an element exists in a tuple isn’t enough ,we want to know where it appears for the first time in the tuple. A member function index is provided by Tuple ()
It returns the index of the first occurrence of x in the tuple. In addition, if an element is not found in a tuple, an exception ValueError is thrown.
Below is the implementation:
# given tuple given_tuple = (3, 4, 5, 6, 2) # given element element = 2 try: # calculating the element's index elem_index = given_tuple.index(element) # printing the position print("The given element is found at the index", elem_index) except: print("The specified element does not exist in the tuple")
Output:
The item is found in the tuple
Related Programs:
- python how to find keys by value in dictionary
- python how to find all indexes of an item in a list
- python how to add an element in list append vs extend
- python how to insert an element at specific index in list
- python program to find the odd occurring element in an array list
- python program to sort a list of tuples in increasing order by the last element in each tuple
- cpp list find contains how to search an element in stdlist