Tuples Methods In Python
In this lesson, we’ll discuss Python tuples methods with examples in order to achieve our instructional goals.
Python Tuples Methods
There are several built-in Python tuple methods that you can use when dealing with Python tuples.
Method | Overview |
---|---|
count() | Tuple that returns the number of times a given value appears. |
index() | Returns the position of the value when the tuple is searched for a given value. |
Python Tuple index() Method
Using index(), you can find the first instance of the given value.
Exceptions are thrown by the index() method if the value cannot be found.
Syntax:
tuple.index(value)
You can find the position of the first occurrence of the value “Los Angeles Rams” by searching for it:
Example: 
By searching for it, you can find the position of the first appearance of “Arizona Cardinals” and “Kansas City Chiefs”:
Example: 
Tip: See our chapter section on tuple index() method for more information.
Python Tuple count() Method
A tuple’s count() method returns the number of times a specified value appears.
Syntax:
tuple.count(value)
Find out how many times the value “Dallas Cowboys” occurs in the tuple:
Example: 
Find out how many times the values “Dallas Cowboys” and “Houston Texans” occur in the tuple and also find the total duplicates in the two tuples:
Example: 
Recommendation: Visit our chapter section on tuple count() method to learn more.
Python Tuple Methods Importance
Here are some important tuple methods:
- This method returns the number of occurrences of a specific element within a tuple. It allows you to quickly determine how many times a particular value appears in the tuple.
- The
index()
method returns the index of the first occurrence of a specified element in the tuple. It helps you find the position of a value within the tuple. - Although not a specific tuple method,
len()
is a built-in Python function that can be used to determine the length of a tuple. It returns the number of elements in the tuple. - The
tuple()
method is used to create a new tuple from an iterable object, such as a list, string, or another tuple. It allows you to convert other data types to tuples. - While not a method itself, tuples support iteration using loops. You can use a
for
loop to iterate over the elements of a tuple, accessing each element for processing.