Get the length of an Array in Python

avatar

Borislav Hadzhiev

Last updated: Jun 14, 2022

banner

Photo from Unsplash

Get the length of an Array in Python #

Use the len() function to get the length of an array, e.g. len(my_array). The len() function returns the length (the number of items) of an object and can be passed a sequence (an array, a list, string, tuple, range or bytes) or a collection (a dictionary, set, or frozen set).

main.py
# 👇 get length of a list my_list = ['a', 'b', 'c'] result_1 = len(my_list) print(result_1) # 👉️ 3 # 👇️ get length of list inside of a list my_2d_list = [['a', 'b'], ['c', 'd']] result_2 = len(my_2d_list[0]) print(result_2) # 👉️ 2

If you need to get the length of a numpy array, scroll down to the next code snippet.

The len() function returns the length (the number of items) of an object.

The first example shows how to get the number of items in the list.

If you have a list of lists (two-dimensional list), make sure to access the list at a specific index to get the length of a nested list.

If you need to get the number of elements in a numpy array, use the size attribute on the array.

main.py
import numpy as np arr = np.array([1, 2, 3]) print(arr.size) # 👉️ 3 print(arr.shape) # 👉️ (3, ) my_2d_arr = np.array([[1, 2, 3], [4, 5, 6]]) print(my_2d_arr.size) # 👉️ 6 print(my_2d_arr.shape) # 👉️ (2, 3)

The size attribute returns the number of elements in the numpy array.

The shape attribute returns a tuple of the array's dimensions.

The first array in the example is a one-dimensional array containing 3 elements.

The second array is two-dimensional and contains 2 nested arrays with 3 elements each.

The first value in the tuple is the number of rows, and the second - the number of columns in the array.

You can also use the len() function to check if a list is empty.

main.py
my_list = [] if len(my_list) == 0: # 👇️ this runs print('list is empty') else: print('list is not empty')

If a list has a length of 0, then it's empty.

You might also see examples online that check whether the list is truthy (to check if it contains at least 1 item), which is more implicit.

main.py
my_list = [] if my_list: print('list is NOT empty') else: # 👇️ this runs print('list is empty')

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

Notice that an empty list is a falsy value, so if the list is empty, the else block is run.

If you need to check if the list is empty using this approach, you would negate the condition with not.

main.py
my_list = [] if not my_list: # 👇️ this runs print('list is empty') else: print('list is NOT empty')
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.