Python len()

Summary: in this tutorial, you will learn how to use the Python len() function to get the number of items of an object.

Introduction to the Python len() function

The len() function returns the number of items (length) of an object. Here’s the syntax of the len() function:

len(s)Code language: Python (python)

In this syntax, the s is an object that you want to get the length. The object can be:

  • A sequence such as a string, bytes, tuple, list or range.
  • A collection such as a dictionary, a set or a frozen set.
  • Or any user-defined object that implements the __len__() method.

When you call the len() function on an object, the len() function will delegate to __len__ method of the object. In other words, it’ll call the __len__() method of the object.

So the following function call:

len(obj)Code language: Python (python)

is equivalent to:

obj.__len__()Code language: Python (python)

Python len() function examples

Let’s take some examples of using Python len() function.

1) Using the len() function with a sequence

The following example shows how to use the len() function to get the length of a string, the number of elements in a tuple, and the number of items in a range:

s = 'Python'
print(len(s))  # 👉 6

seasons = ('Spring', 'Summer', 'Autumn', 'Winter')
print(len(seasons))  # 👉 4


r = range(1, 10)
print(len(r))  # 👉 9Code language: Python (python)

2) Using the len() function with a collection

The following example uses the len() function to get the number of items in a collection:

days = {
    1: 'Sun',
    2: 'Mon',
    3: 'Tue',
    4: 'Wed',
    5: 'Thu',
    6: 'Fri',
    7: 'Sat'
}
print(len(days))  # 👉 7

rgb = set(('red', 'green', 'blue'))
print(len(rgb))  # 👉 3Code language: Python (python)

3) Using the len() function with user-defined object

The following example shows how to use the len() function with a user-defined object:

class Employee:
    def __init__(self, name):
        self.name = name
        self.dependents = []

    def add_dependent(self, dependent):
        self.dependents.append(dependent)
        return self

    def __len__(self):
        return len(self.dependents)


employee = Employee('John Doe')
employee.add_dependent('Alice Doe')
employee.add_dependent('Bob Doe')

print(len(employee))  # 👉 2Code language: Python (python)

How it works.

First, define the Employee class that has two attributes name and dependents:

class Employee:
    def __init__(self, name):
        self.name = name
        self.dependents = []Code language: Python (python)

Next, define the add_dependent() that adds a dependent to the dependents list:

def add_dependent(self, dependent):
   self.dependents.append(dependent)
   return selfCode language: Python (python)

Then, implement the __len__() method that returns the number of dependents of the employee.

def __len__(self):
   return len(self.dependents)Code language: Python (python)

After that, create an Employee object and add two dependents:

employee = Employee('John Doe')
employee.add_dependent('Alice Doe')
employee.add_dependent('Bob Doe')Code language: Python (python)

Finally, pass the employee object to the len() function. The len() will call the __len__() method of the Employee class.

Summary

  • Use the len() function to get the number of items of an object.
  • The len() function delegates the call to __len__() method.
Did you find this tutorial helpful ?