How to Find the Length of a Tuple in Python?

Here are the various ways to find the length of a tuple in Python. As a data scientist working with large datasets of US demographics, I often need to determine the size of tuples containing data like state populations, median incomes, and more. In this article, I will explain important methods to accomplish this task with examples.

Find the Length of a Tuple in Python

Before we get into finding the length, let’s review what a tuple is in Python. A tuple is a collection of Python objects that are separated by commas enclosed between parentheses. Tuples are similar to lists, but they are immutable, meaning their contents cannot be changed once defined. Tuples are often used to store related pieces of data.

For example, let’s say we have a tuple containing the populations of some major US cities:

city_populations = ("New York", 8336817, "Los Angeles", 3979576, "Chicago", 2693976)

Read Python Set vs Tuple

Use the len() Function to Find Tuple Length

The most simple way to find the length of a tuple in Python is by using the built-in len() function. The len() function in Python returns the number of elements in the tuple.

Here’s an example of using len() to find the length of our city_populations tuple:

city_populations = ("New York", 8336817, "Los Angeles", 3979576, "Chicago", 2693976)
length = len(city_populations)
print(f"The tuple has {length} elements.")

Output:

The tuple has 6 elements.

I executed the above example code and added the screenshot below.

Find the Length of a Tuple in Python

As we can see, the len() function quickly tells us that the city_populations tuple contains 6 elements.

Check out How to Unpack a Tuple in Python?

Find the Length of Tuple Items

In some cases, you may want to find the length of the individual items within a Python tuple. For example, let’s say we have a tuple of US state names:

states = ("California", "Texas", "Florida", "New York", "Pennsylvania")

To find the length of each state name, we can use a combination of len() and a for loop:

states = ("California", "Texas", "Florida", "New York", "Pennsylvania")
for state in states:
    print(f"{state} has {len(state)} characters.")

Output:

California has 10 characters.
Texas has 5 characters.
Florida has 7 characters.
New York has 8 characters.
Pennsylvania has 12 characters.

I executed the above example code and added the screenshot below.

How to Find the Length of a Tuple in Python

This technique uses the for keyword to create a generator expression that applies len() to each item in the tuple.

Read How to Convert an Array to a Tuple in Python?

Find the Length of the Longest Item in a Tuple

Another common task is finding the length of the longest item in a tuple. We can achieve this by combining len() with the max() function in Python.

Using our states tuple from earlier:

states = ("California", "Texas", "Florida", "New York", "Pennsylvania") 
longest_state = max(states, key=len)
print(f"The longest state name is {longest_state} with {len(longest_state)} characters.")

Output:

The longest state name is Pennsylvania with 12 characters.

I executed the above example code and added the screenshot below.

Find the Length of a Longest Item Tuple in Python

Here, max() finds the longest string in the tuple, while len() determines its length.

Check out How to Iterate Through Tuples in Python?

Conclusion

In this tutorial, I explained how to find the length of a tuple in Python. I discussed using the len() function to find tuple length, finding the length of the tuple items, and finding the length of the longest item in a tuple.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.