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.

MethodOverview
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: 

nfl_teams = ("Dallas Cowboys", "Arizona Cardinals","Dallas Cowboys", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams")mrx = nfl_teams.index("Los Angeles Rams")print(mrx)

By searching for it, you can find the position of the first appearance of “Arizona Cardinals” and “Kansas City Chiefs”:

Example: 

nfl_teams = ("Dallas Cowboys", "Arizona Cardinals","Dallas Cowboys", "Chicago Bears", "Atlanta Falcons","Kansas City Chiefs", "Dallas Cowboys", "Los Angeles Rams")mrx = nfl_teams.index("Arizona Cardinals") ample = nfl_teams.index("Kansas City Chiefs")print(mrx) print(ample)

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: 

nfl_teams = ("Dallas Cowboys", "Arizona Cardinals","Dallas Cowboys", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams")mrx = nfl_teams.count("Dallas Cowboys")print(mrx)

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: 

nfl_teams1 = ("Dallas Cowboys", "Arizona Cardinals","Dallas Cowboys", "Chicago Bears", "Atlanta Falcons", "Dallas Cowboys", "Los Angeles Rams") nfl_teams2 = ("Kansas City Chiefs", "Atlanta Falcons", "Houston Texans", "Arizona Cardinals", "Atlanta Falcons", "Houston Texans") mrx = nfl_teams1.count("Dallas Cowboys") ample = nfl_teams2.count("Houston Texans") print(mrx) print(ample) print(f"Total duplicates = {mrx + ample}")

Recommendation: Visit our chapter section on tuple count() method to learn more.


Python Tuple Methods Importance

Here are some important tuple methods:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
Help us improve! Leave your reaction below to appreciate our efforts or provide suggestions for enhancing this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *