How to Find the Size (Length) of a List in Python

To find the size of a list in Python, use len(): len(["Austin", "Denver", "Seattle"]) returns 3, the number of elements. The same call gives the length of a list, and it’s instant because Python stores the count with the list:

cities = ["Austin", "Denver", "Seattle", "Boston", "Tampa"]
len(cities)   # 5

“Size” can also mean memory, so I’ve covered both meanings below, each with output from Python 3.12.5.

You’ll see len() with empty lists, nested lists, len vs count(), why map objects have no length, the size of a list in bytes, and why len() costs the same for any list.

Find the number of elements in a list with len()

len() returns how many items the list holds. An empty list has length 0, which is why if not orders: works as an empty check:

cities = ["Austin", "Denver", "Seattle", "Boston", "Tampa"]
print(len(cities))
print(len([]))

orders = []
if not orders:                       # the usual empty check
    print("no orders yet")

print(cities[len(cities) - 1], cities[-1])   # last item two ways

Output:

5
0
no orders yet
Tampa Tampa
Command Prompt output of Python len() returning 5 for a list of cities and 0 for an empty list, plus the last item
The city list has 5 items, the empty list 0, the empty orders list triggers the message, and both last-item lookups return Tampa.

len(cities) - 1 is the last index; -1 is shorter. More ways to grab the end are in getting the last element of a list.

Find the length of a nested list

len() only counts top-level items, so an inner list counts as one. Add up the inner lengths, or recurse for deeper nesting:

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 deep_len(items):
    return sum(deep_len(x) if isinstance(x, list) else 1 for x in items)

print("deep count:", deep_len([1, [2, 3, [4, 5]], 6]))

Output:

top-level items: 4
cities in total: 6
lengths: [3, 1, 2, 0]
deep count: 6
Command Prompt output of Python len() on a list of lists versus the total number of cities and a recursive deep count
The regions list has 4 top-level items but 6 cities, and the recursive count finds 6 values in the deeper list.

Empty inner lists still count as top-level items. The same rules apply to tuples, as shown in finding the length of a tuple.

len() vs count(), and why map objects have no length

count(x) tells you how often one value appears, which is a different question. Lazy objects like map and generators don’t know their length at all:

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

words = ["Python", "list", "size"]
print("items:", len(words), "| characters:", sum(len(w) for w in words))

squares = map(lambda n: n * n, range(4))
try:
    len(squares)
except TypeError as err:
    print("TypeError:", err)
print(len(list(squares)))

Output:

len: 7 | count(5): 3 | distinct: 4
items: 3 | characters: 14
TypeError: object of type 'map' has no len()
4
Command Prompt output of Python list len versus count and set, total characters in a list of words, and TypeError for len of a map object
The ratings list has 7 items, three 5s and 4 distinct values; the words hold 14 characters; len() on a map object raises TypeError.

Converting with list() uses the map up, so keep the list. Counting every value at once is covered in counting the frequency of elements in a list.

How do I get the size of a list in bytes?

sys.getsizeof() reports the memory of the list object itself: its header plus one pointer per slot. It doesn’t include the objects the list points to:

import sys

small = [1, 2, 3]
names = ["Emily Carter" * 100, "James Wilson"]

print(len(small), "items,", sys.getsizeof(small), "bytes for the list object")
print(len(names), "items,", sys.getsizeof(names), "bytes for the list object")
print("plus the strings:", sum(sys.getsizeof(s) for s in names), "bytes")

growing = []
for i in range(10):
    growing.append(i)
    print(len(growing), sys.getsizeof(growing), end=" | ")
print()

Output:

3 items, 88 bytes for the list object
2 items, 72 bytes for the list object
plus the strings: 1294 bytes
1 88 | 2 88 | 3 88 | 4 88 | 5 120 | 6 120 | 7 120 | 8 120 | 9 184 | 10 184 |
Command Prompt output of Python sys.getsizeof showing list object size in bytes, string sizes and the size jumping as a list grows
Both two- and three-item lists report a small fixed size even when one string is long, and the growing list’s size jumps in steps rather than per item.

Lists reserve extra slots as they grow, so memory goes up in jumps. If you know the size ahead of time, see initializing a list of size N.

Is len() slow on large lists?

No. The count is stored on the list, so len() doesn’t loop. Ten items and a million items take about the same time:

import timeit

for n in (10, 1_000_000):
    data = list(range(n))
    t = min(timeit.repeat("len(data)", globals={"data": data}, number=1_000_000, repeat=3))
    print(f"len() on {n:>9,} items: {t * 1000:.1f} ms per million calls")

Output:

len() on        10 items: 19.7 ms per million calls
len() on 1,000,000 items: 29.7 ms per million calls

The timings vary by machine and wobble a little, but they don’t grow a million-fold with the list. For strings, the same holds, as covered in getting the length of a string in Python.

List size methods compared

QuestionCode
How many elements?len(lst)
Is it empty?if not lst:
Total in nested listssum(len(x) for x in lst)
How often does x appear?lst.count(x)
How many distinct values?len(set(lst))
Memory of the list objectsys.getsizeof(lst)

Here’s where to go next with lists:

Frequently asked questions

How do I find the size of a list in Python?

Use len(my_list). It returns the number of elements.

What’s the difference between list size and length?

In everyday Python they mean the same thing: len(). Size in bytes is sys.getsizeof().

Does len() count items inside nested lists?

No, only top-level items. Use sum(len(x) for x in nested) for one level.

Why does len() fail on a map object?

map and generators are lazy and don’t know their length. Convert with list() first.

Is len() O(1) in Python?

Yes. Lists store their length, so len() takes the same time for any size.

Where is len() documented?

See len() in the Python docs.