In Python, the built-in function len() is used to get the length (the number of items) of a list.
- Built-in Functions - len() — Python 3.9.7 documentation
This article describes the following contents.
- Get the number of items in a list with len()
- For two-dimensional lists (lists of lists)
- For multidimensional lists
See the following article for the usage of len() for objects of other types.
- How to use len() in Python
You can get the total number of elements with len(). If you want to get the number of occurrences of an element, use the count() method or Counter of the standard library collections.
- Count elements in a list with collections.Counter in Python
Get the number of items in a list with len()
By passing a list as an argument of the built-in function len(), its number of items is returned as an integer value.
l = [0, 1, 2, 3] print(len(l)) # 4
For two-dimensional lists (lists of lists)
If a two-dimensional list (a list of lists) 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
The number of items in each list (the number of items in each row) can be obtained using the list comprehensions.
- List comprehensions in Python
print([len(v) for v in l_2d]) # [3, 3]
The total number of items can be calculated with sum().
A generator version of the list comprehensions (= generator expressions) is used here. Generator expressions are enclosed in (), not [], but when they are used within (), as in this example, it is not necessary to write () twice, and () of generator expressions can be omitted.
print(sum(len(v) for v in l_2d)) # 6
If NumPy is installed in your environment, you can convert it to a NumPy array ndarray.
You can get the total number of items with the attribute size, and the shape (number of rows and columns) with the attribute shape.
- Convert numpy.ndarray and list to each other
- NumPy: Get the number of dimensions, shape, and size of ndarray
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)
For multidimensional lists
Take as an example a list whose elements are lists of varying sizes.
If this is passed to len(), the number of objects stored as elements is returned.
l_multi = [[0, 1, 2, [10, 20, 30]], [3, 4, 5], 100] print(len(l_multi)) # 3
Also, if you pass it to numpy.array(), a NumPy array ndarray whose elements are list type objects with varying numbers of elements is generated. size and shape are also calculated for objects stored as elements.
arr_multi = np.array(l_multi) print(arr_multi) # [list([0, 1, 2, [10, 20, 30]]) list([3, 4, 5]) 100] print(arr_multi.size) # 3 print(arr_multi.shape) # (3,)
To get the total number of elements in a list that stores lists of varying sizes, 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