How to get the length of a list in Python

Share

A list is a data structure in Python and, like other classes, it has a header in its implementation. However, this header also includes the implementation of the method len() which returns the length of the list.

svg viewer

Syntax

len(obj)

The parameter may be a sequence (i.e., string, bytes, tuple, list, or range) or a collection (i.e., a dictionary or a set).

len() returns the length of an object; t​he length is the same as the number of items in an object.

Code

my_list = [1,2,3,4,5]
len_my_list = len(my_list)
print("The length of my_list is",len_my_list)
Copyright ©2024 Educative, Inc. All rights reserved