In this Python tutorial, you will learn how to find the length of a list using the built-in len() function. The length of a list is the number of top-level elements it contains.

Find the Length of a List in Python Using len()

To find the length of a Python list, pass the list to the built-in len() function. The function returns an integer representing the number of elements in the list.

</>
Copy
len(list_name)

For example, len([10, 20, 30]) returns 3. The values and data types stored in the list do not affect how the elements are counted.

Python List Length Examples

1. Find the Length of a List of Integers

In the following Python program, we initialize a list with integers and use len() to find the number of elements in the list.

Python Program

</>
Copy
#initialize list
aList = [21, 28, 14, 96, 84, 65, 74, 31]

#find list length
len = len(aList)

#print list length
print('Length of the list is :', len)

Output

Length of the list is : 8

The list contains eight integer elements, so len(aList) returns 8.

Note: The example above stores the result in a variable named len. In new programs, use a name such as list_length instead, because assigning a value to len hides Python’s built-in len() function in the current scope.

Reference tutorials for the above program

2. Find the Length of a List with Different Data Types

A Python list may contain values of different data types. The len() function counts each top-level element once, regardless of whether the element is a Boolean, integer, string, or another object.

Python Program

</>
Copy
#initialize list
aList = [True, 28, 'Tiger']

#find list length
len = len(aList)

#print list length
print('Length of the list is :', len)

Output

Length of the list is : 3

The list contains three elements: True, 28, and 'Tiger'. Therefore, its length is 3.

3. Find the Length of an Empty Python List

An empty list contains no elements, so its length is zero. Calling len() on an empty list returns 0.

Python Program

</>
Copy
#empty list
aList = []

#find list length
len = len(aList)

#print list length
print('Length of the list is :', len)

Output

Length of the list is : 0

4. Store the Python List Length Without Hiding len()

Use a descriptive variable name when storing the value returned by len(). This keeps the built-in function available for later calls.

</>
Copy
fruits = ['apple', 'banana', 'cherry']

list_length = len(fruits)

print('Length of the list is :', list_length)

Output

Length of the list is : 3

How len() Counts Elements in a Nested Python List

For a nested list, len() counts only the elements in the outer list. An inner list is treated as one element, regardless of how many values it contains.

</>
Copy
nested_list = [[1, 2], [3, 4, 5], 6]

print(len(nested_list))
print(len(nested_list[1]))

Output

3
3

The outer list has three elements: two inner lists and the integer 6. The second inner list also has three elements.

Check Whether a Python List Is Empty with len()

You can compare the list length with zero to check whether a list is empty.

</>
Copy
items = []

if len(items) == 0:
    print('The list is empty.')
else:
    print('The list contains elements.')

Output

The list is empty.

In Python, the shorter form if not items: is also commonly used to test whether a list is empty.

Python List Length Questions

What does len() return for a Python list?

len() returns an integer equal to the number of top-level elements in the list.

Does len() count list indexes from zero?

No. List indexing starts at zero, but the length is the total number of elements. A list with indexes 0, 1, and 2 has a length of 3.

Does len() modify the original list?

No. The len() function reads the number of elements and leaves the original list unchanged.

Does len() count every value inside nested lists?

No. It counts the elements in the list passed to it. Each nested list is counted as one element unless you call len() on that inner list separately.

Summary of Finding Python List Length

In this Python Tutorial, we learned how to find the length of a list using the len() function. We also covered empty lists, mixed data types, nested lists, safe variable naming, and checking whether a list contains any elements.