Last modified: May 25, 2026

Python len Function: Simple Guide

The len function is a built-in Python tool. It returns the number of items in an object. You can use it with strings, lists, tuples, dictionaries, and sets. It is one of the most useful functions in Python.

What Does len Do?

len stands for length. It gives you the count of elements in a container. For a string, it counts characters. For a list, it counts items. The function is fast and simple to use.

Here is the basic syntax:

 
length = len(object)

The object can be a sequence or collection. The result is always an integer.

Using len with Strings

Strings are sequences of characters. len counts every character, including spaces and punctuation.

 
# Example with a string
text = "Hello, World!"
length = len(text)
print(length)  # Output: 13

13

Notice the comma and space are counted. This is helpful for validation, like checking password length.

Using len with Lists

Lists are ordered collections. len returns the number of elements in the list.

 
# Example with a list
fruits = ["apple", "banana", "cherry"]
print(len(fruits))  # Output: 3

3

An empty list has a length of zero. This is useful when working with dynamic data.

If you need to add items to a list, check out the Python List Insert: Complete Guide for more details.

Using len with Tuples

Tuples are similar to lists but immutable. len works the same way.

 
# Example with a tuple
coordinates = (10, 20, 30)
print(len(coordinates))  # Output: 3

3

Using len with Dictionaries

For dictionaries, len returns the number of key-value pairs.

 
# Example with a dictionary
student = {"name": "Alice", "age": 25, "grade": "A"}
print(len(student))  # Output: 3

3

It only counts keys, not values. This helps you track how many entries a dictionary has.

Using len with Sets

Sets are unordered collections of unique items. len returns the number of unique elements.

 
# Example with a set
numbers = {1, 2, 3, 3, 4}
print(len(numbers))  # Output: 4 (duplicate 3 is removed)

4

Common Use Cases for len

You can use len in loops, conditionals, and data validation. Here are some examples:

Check if a list is empty:

 
items = []
if len(items) == 0:
    print("The list is empty.")

The list is empty.

Iterate over a range using length:

 
names = ["John", "Jane", "Joe"]
for i in range(len(names)):
    print(names[i])

John
Jane
Joe

Validate user input length:

 
password = "secret123"
if len(password) < 8:
    print("Password too short!")
else:
    print("Password is valid.")

Password is valid.

What About Other Data Types?

len works with any object that implements the __len__ method. This includes strings, lists, tuples, dictionaries, sets, and custom classes. If you try len on an integer, you get a TypeError.

 
# This will cause an error
# print(len(42))  # TypeError: object of type 'int' has no len()

Always make sure the object is a sequence or collection.

Performance of len

The len function is very fast. It runs in constant time, O(1). This means it takes the same amount of time regardless of the object size. Python stores the length internally for most built-in types.

You do not need to worry about performance when using len.

Common Mistakes with len

Beginners sometimes confuse len with indexing. Remember, len gives the count, not the last index. The last index is always len(object) - 1.

 
# Correct way to access last element
my_list = [10, 20, 30]
last_index = len(my_list) - 1
print(my_list[last_index])  # Output: 30

30

Another mistake is using len on a generator. Generators do not have a length. Convert it to a list first if needed.

Related Functions and Methods

Python has other functions that work well with len. For example, range(len()) is common in loops. You can also use len to check if a list needs more items before using Python List Insert at End.

If you are working with strings, len helps you split or join them. For joining lists, see Python List Join: Complete Guide.

Conclusion

The len function is simple but powerful. It helps you find the size of strings, lists, tuples, dictionaries, and sets. Use it for validation, loops, and data checks. It is fast, reliable, and easy to remember. Practice with different data types to master it. Now you can confidently use len in your Python projects.