len()
Returns how many items something holds: characters in a string, elements in a list or tuple, keys in a dictionary or members of a set.
len(s)len() tells you the size of a collection. For a string it counts characters, for a list or tuple it counts elements, for a dictionary it counts keys and for a set it counts members.
You use it to check that something is not empty, to validate input ("a password needs at least 8 characters"), or to work out averages and percentages.
Two things surprise people. First, len() counts characters, not bytes: "café" has 4 characters even though it takes 5 bytes when saved as UTF-8. Second, it only counts the top level: a list that holds two lists has a length of 2, however many items are inside them.
Parameters
| Parameter | What it is |
|---|---|
srequired | A string, list, tuple, dictionary, set, range or any other object that knows its own size. Numbers do not have a length. |
Return value
A whole number (int), 0 or more.
Examples
1Length of a string, a list and a dictionary
print(len("Python"))
print(len(["Lisbon", "Oslo", "Lima"]))
print(len({"apples": 3, "pears": 5}))
print(len(""))6 3 2 0
A dictionary's length is the number of keys. An empty string has length 0.
2Check a password's length
password = "sunny42"
if len(password) < 8:
print(f"Too short: {len(password)} characters, need at least 8")
else:
print("Password accepted")Too short: 7 characters, need at least 8
3Work out an average
scores = [72, 88, 95, 64]
average = sum(scores) / len(scores)
print(f"{len(scores)} tests, average {average:.1f}")4 tests, average 79.8
4Characters are not bytes
Letters with accents and emoji count as one character each, but take more than one byte when encoded.
word = "café"
print(len(word))
print(len(word.encode("utf-8")))4 5
5Only the top level is counted
teams = [["Ana", "Ben"], ["Chen", "Dara", "Eli"]]
print(len(teams))
print(sum(len(team) for team in teams))2 5
To count everything inside, add up the length of each inner list.
Common mistakes
Calling len() on a number
Numbers have no length. To count the digits, turn the number into a string first.
year = 2026
try:
print(len(year))
except TypeError:
print("TypeError: an int has no len()")
print(len(str(year)))TypeError: an int has no len() 4
Using len() as the last index
Positions start at 0, so the last item is at len(items) - 1, not len(items). Even simpler: use items[-1].
items = ["a", "b", "c"]
try:
print(items[len(items)])
except IndexError:
print("IndexError: there is no position 3")
print(items[len(items) - 1], items[-1])IndexError: there is no position 3 c c
Questions people ask
How do I check if a list is empty in Python?
if not items: is the usual way, because an empty list counts as false. if len(items) == 0: also works but is longer.
How do I count words in a string?
Split it first: len(text.split()). len(text) would count characters.
Is len() slow on big lists?
No. Lists, strings, dictionaries and sets store their size, so len() answers at once however large they are.
Why does len() not work on a generator or map object?
They produce items one by one and do not know in advance how many there will be. Turn them into a list first: len(list(gen)).