Python List Length (len)

Learn how to get the number of items in a Python list using len(). This page focuses on the basic use of len() with lists, common beginner mistakes, and simple examples.

numbers = [10, 20, 30]
count = len(numbers)
print(count)

Output:

3

Use len(list_name) to get how many items are in a list.

What this page covers

  • How len() works with Python lists
  • What value len() returns
  • Simple examples with empty and non-empty lists
  • Common mistakes beginners make

What len() returns for a list

len(my_list) returns an integer.

That integer is the number of items in the list.

A few important points:

  • It counts list items
  • It does not count characters inside a string item
  • An empty list returns 0

Example:

words = ["cat", "elephant", "dog"]
print(len(words))

Output:

3

Even though "elephant" has many characters, it still counts as just one item in the list.

If you want a broader explanation of the function itself, see Python len() function explained.

Basic example

Create a list, pass it to len(), and print the result.

colors = ["red", "green", "blue"]
count = len(colors)

print(count)

Output:

3

What happens here:

  • colors is a list with 3 items
  • len(colors) returns 3
  • That value is stored in count

You can also print the result directly:

colors = ["red", "green", "blue"]
print(len(colors))

Empty list example

An empty list has no items, so its length is 0.

items = []
print(len(items))

Output:

0

This is useful when you want to check whether a list has anything in it before using an index.

items = []

if len(items) > 0:
    print(items[0])
else:
    print("The list is empty")

Output:

The list is empty

If you are still learning how lists work, Python lists explained for beginners is a good next step.

Nested lists

len() counts only the top-level items in a list.

A nested list counts as one item.

data = [[1, 2], [3, 4], [5, 6]]
print(len(data))

Output:

3

Here, the list contains 3 items, and each item is itself a list.

Another example:

mixed = [1, [2, 3], 4]
print(len(mixed))

Output:

3

Even though [2, 3] contains 2 values, it is still only one item in the outer list.

Common beginner mistakes

Using my_list.len() instead of len(my_list)

This is a very common mistake.

Wrong:

numbers = [10, 20, 30]
print(numbers.len())

Correct:

numbers = [10, 20, 30]
print(len(numbers))

len() is a built-in function. It is not a list method.

Forgetting parentheses

Wrong:

numbers = [10, 20, 30]
print(len)

This prints the function itself, not the list length.

Correct:

numbers = [10, 20, 30]
print(len(numbers))

Confusing length with the last index

The length of a list is not the same as the last index.

Example:

letters = ["a", "b", "c"]
print(len(letters))

Output:

3

But the last index is 2, not 3.

Indexes are:

  • 0 for "a"
  • 1 for "b"
  • 2 for "c"

So this causes an error:

letters = ["a", "b", "c"]
print(letters[len(letters)])

Why? Because len(letters) is 3, and index 3 does not exist.

Use this instead:

letters = ["a", "b", "c"]
print(letters[len(letters) - 1])

Output:

c

If you see an index problem here, read IndexError: list index out of range.

Using len() on the wrong variable

Sometimes the code is correct, but the variable is not what you expect.

Use these debugging checks:

print(my_list)
print(len(my_list))
print(type(my_list))
print(my_list[0])

These help you verify:

  • what the variable contains
  • how many items it has
  • whether it is really a list
  • what the first item is

Be careful with print(my_list[0]) if the list might be empty.

When to use len() with lists

You will often use len() in real programs.

Common uses include:

  • Check if a list is empty
  • Control loops
  • Validate input or results
  • Compare the sizes of two lists

Example: comparing two lists

a = [1, 2, 3]
b = [10, 20]

print(len(a))
print(len(b))
print(len(a) > len(b))

Output:

3
2
True

Example: using length in a loop

names = ["Ana", "Ben", "Cara"]

for i in range(len(names)):
    print(i, names[i])

Output:

0 Ana
1 Ben
2 Cara

This works, but if you want to practice looping through lists more safely and clearly, see how to loop through a list in Python.

You can also read how to get the length of a list in Python for a task-focused guide.

FAQ

How do I get the length of a list in Python?

Use len(your_list). It returns the number of items in the list.

What does len([]) return?

It returns 0 because the list is empty.

Is the list length the same as the last index?

No. The last index is one less than the length.

Can I use my_list.len() in Python?

No. Use len(my_list), because len() is a built-in function.

See also