Get the Size (Length, Number of Items) of a List in Python

Modified: | Tags: Python, List

In Python, you can get the size (length, number of items) of a list using the built-in len() function.

You can get the total number of items using len(). If you want to count the number of occurrences of an item, use the count() method or the Counter class of the collections module.

See the following article for how to use len() with objects of other types.

Get the size of a list with len()

Passing a list as an argument to the built-in len() function returns its size (length, number of items) as an integer value.

l = [0, 1, 2, 3]

print(len(l))
# 4
source: list_len.py

Get the size of a list of lists (2D list)

If a list of lists (2D list) is passed directly to len(), the number of lists stored as elements is returned.

l_2d = [[0, 1, 2], [3, 4, 5]]

print(len(l_2d))
# 2
source: list_len.py

The number of items in each list (the number of items in each row) can be obtained using list comprehensions.

print([len(v) for v in l_2d])
# [3, 3]
source: list_len.py

The total number of items can be calculated with sum().

Generator expressions are used here, which are similar to list comprehensions but enclosed in (). In this example, the outer pair of parentheses can be omitted since the expression is already within another pair.

print(sum(len(v) for v in l_2d))
# 6
source: list_len.py

If NumPy is installed in your environment, you can convert a list of lists to a NumPy array (numpy.ndarray).

You can get the total number of items with the size attribute, and the shape (number of rows and columns) with the shape attribute.

import numpy as np

l_2d = [[0, 1, 2], [3, 4, 5]]
arr_2d = np.array(l_2d)

print(arr_2d)
# [[0 1 2]
#  [3 4 5]]

print(arr_2d.size)
# 6

print(arr_2d.shape)
# (2, 3)
source: list_len.py

Get the size of a list with lists and non-list items

Consider a list with lists and non-list items.

l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100]
source: list_len.py

See the following article on how to flatten such a list to one dimension.

If this list is passed to len(), the number of objects stored as elements is returned.

print(len(l_multi))
# 3
source: list_len.py

Passing this list to numpy.array() causes an error (as of version 1.25.1). Older versions may generate a NumPy array (numpy.ndarray) with list elements.

# arr_multi = np.array(l_multi)
# ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions.
# The detected shape was (3,) + inhomogeneous part.
source: list_len.py

To get the total number of elements in a list with lists and non-list items, you can define a function that recursively calculates the number of elements in the list.

def my_len(l):
    count = 0
    if isinstance(l, list):
        for v in l:
            count += my_len(v)
        return count
    else:
        return 1

l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100]
print(my_len(l_multi))
# 10

l_2d = [[0, 1, 2], [3, 4, 5]]
print(my_len(l_2d))
# 6

l = [0, 1, 2, 3]
print(my_len(l))
# 4
source: list_len.py

The isinstance() function is used to determine if an object is a list.

Note that passing an object other than a list will return 1.

print(my_len(100))
# 1
source: list_len.py

For example, to return None for an object that is not a list, you can define a wrapper function as follows.

def my_len_wrapper(l):
    if isinstance(l, list):
        return my_len(l)
    else:
        return None

print(my_len_wrapper(l_multi))
# 10

print(my_len_wrapper(l_2d))
# 6

print(my_len_wrapper(l))
# 4

print(my_len_wrapper(100))
# None
source: list_len.py

Related Categories

Related Articles