How to Find the Length of a Tuple in Python

To find the length of a tuple in Python, pass it to len(): len(("Austin", "TX", 78701)) returns 3. It counts the top-level items only, so a nested tuple counts as one item, and it works the same for empty and single-item tuples:

store = ("Austin", "TX", 78701, 18250.50)
len(store)   # 4

Every result below was printed by Python 3.12.5, including the two TypeErrors that surprise people.

You’ll see len() on normal, empty and one-element tuples, why (5) has no length, counting items in nested tuples, len vs count(), named tuples and generators, and checking length before you unpack.

Find the length of a tuple with len()

len() reads a stored size, so it’s instant no matter how big the tuple is. Parentheses are optional; the commas make the tuple:

store = ("Austin", "TX", 78701, 18250.50)
print(len(store))
print(len(()))                       # empty tuple
print(len(("Denver",)))              # one element needs the comma

coords = 30.27, -97.74               # parentheses are optional
print(len(coords), type(coords).__name__)

Output:

4
0
1
2 tuple
Command Prompt output of Python len() returning the length of a four-item tuple, an empty tuple and a one-element tuple
The store tuple has 4 items, the empty tuple 0, the one-element tuple 1, and the coordinates without parentheses 2.

Checking for zero is the usual empty test; there’s more in checking if a tuple is empty in Python.

Why does len() fail on a one-element tuple?

Because it isn’t a tuple. Parentheses alone just group an expression, so ("Denver") is a string and (5) is an int:

single = ("Denver")                  # just a string in parentheses
print(type(single).__name__, len(single))

number = (5)                         # just an int
try:
    len(number)
except TypeError as err:
    print("TypeError:", err)

print(type((5,)).__name__, len((5,)))

Output:

str 6
TypeError: object of type 'int' has no len()
tuple 1
Command Prompt output of Python len() returning 6 for a string in parentheses and TypeError object of type int has no len()
(“Denver”) is a str with 6 characters, (5) raises object of type ‘int’ has no len(), and (5,) is a tuple of length 1.

The trailing comma is what you need. It’s the most common tuple bug I see, and it’s covered in creating a tuple with one element.

Find the length of a nested tuple

len() doesn’t look inside inner tuples. To count everything, add up the inner lengths, or recurse when the nesting goes deeper:

regions = (("Austin", "Dallas", "Houston"), ("Denver",), ("Tampa", "Miami"))

print("top-level items:", len(regions))
print("cities in total:", sum(len(r) for r in regions))
print("lengths:", [len(r) for r in regions])

def total_items(t):
    """Count every non-tuple item at any depth."""
    return sum(total_items(x) if isinstance(x, tuple) else 1 for x in t)

deep = ("TX", ("Austin", ("78701", "78702")), "CO")
print("len(deep):", len(deep), "| all items:", total_items(deep))

Output:

top-level items: 3
cities in total: 6
lengths: [3, 1, 2]
len(deep): 3 | all items: 5
Command Prompt output of Python len() on a tuple of tuples versus the total number of cities and a recursive item count
The regions tuple has 3 top-level items but 6 cities, and the deeper tuple has 3 top-level items but 5 items in total.

sum(len(r) for r in regions) handles one level; the recursive function handles any depth. Looping over tuples like this is covered in iterating through tuples in Python.

len() vs count(), named tuples and generators

count(x) answers a different question: how many times one value appears. Named tuples work with len() like any tuple, but generators don’t:

from collections import namedtuple

ratings = (5, 4, 5, 3, 5, 4)
print("len:", len(ratings), "| count(5):", ratings.count(5), "| distinct:", len(set(ratings)))

Store = namedtuple("Store", ["city", "state", "revenue"])
s = Store("Austin", "TX", 18250.50)
print(len(s), s._fields)

prices = (19.99 for _ in range(3))           # a generator, not a tuple
try:
    len(prices)
except TypeError as err:
    print("TypeError:", err)
print(len(tuple(prices)))

Output:

len: 6 | count(5): 3 | distinct: 3
3 ('city', 'state', 'revenue')
TypeError: object of type 'generator' has no len()
3
Command Prompt output of Python tuple len versus count, len of a namedtuple, and TypeError for len of a generator
The ratings tuple has 6 items with three 5s and 3 distinct values, the named tuple has length 3, and a generator raises TypeError until it’s turned into a tuple.

Converting a generator with tuple() uses it up, so do it once and keep the result. Named tuples and their alternatives are compared in Python dataclass vs namedtuple.

Check a tuple’s length before unpacking or indexing

Rows from files and APIs don’t always have the fields you expect. Checking len() first turns a crash into a clear message:

def parse_row(row: tuple) -> str:
    if len(row) != 3:
        return f"skipped {row!r}: expected 3 fields, got {len(row)}"
    city, state, total = row
    return f"{city}, {state}: ${total:,.2f}"

rows = [("Austin", "TX", 1250.5), ("Denver", "CO"), ("Tampa", "FL", 980.25, "extra")]
for r in rows:
    print(parse_row(r))

last = rows[0][len(rows[0]) - 1]          # same as rows[0][-1]
print("last field:", last)

Output:

Austin, TX: $1,250.50
skipped ('Denver', 'CO'): expected 3 fields, got 2
skipped ('Tampa', 'FL', 980.25, 'extra'): expected 3 fields, got 4
last field: 1250.5

Without the check, the second row would raise a ValueError on unpacking. If you index past the end instead, you get the error explained in IndexError: tuple index out of range.

Tuple length quick reference

ExpressionResultWhy
len(("A", "B", "C"))3Three items
len(())0Empty tuple
len(("Denver",))1The comma makes the tuple
len(("Denver"))6It’s a string
len(((1, 2), (3,)))2Only top-level items count
len(x for x in t)TypeErrorGenerators have no length

Ready for more tuple practice? Try these:

Frequently asked questions

How do I find the length of a tuple in Python?

Use len(my_tuple). It returns the number of top-level items.

Why does len((5)) raise TypeError?

(5) is just the int 5. Write (5,) to make a one-element tuple.

Does len() count items inside nested tuples?

No. Use sum(len(t) for t in nested) for one level, or a recursive function for deeper nesting.

What’s the difference between len() and count() on a tuple?

len() counts all items; count(x) counts how often one value appears.

Can I get the length of a generator?

No. Convert it with tuple(gen) first, or count with sum(1 for _ in gen).

Where is len() documented?

See len() in the Python docs.