Get the length of a List in Python

avatar

Borislav Hadzhiev

Tue Jun 14 20222 min read

Get the length of a List in Python #

Use the len() function to get the length of a list, e.g. result = len(my_list). The len() function returns the length (the number of items) of an object and can be passed a sequence (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

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.

Indices are zero-based, so the index of the first item in the list is 0, and the index of the last item is len(my_list) - 1.

The last item in the list can also be accessed using -1.

main.py
my_list = ['a', 'b', 'c'] print(len(my_list)) # 👉️ 3 print(my_list[0]) # 👉️ 'a' print(my_list[-1]) # 👉️ 'c' print(my_list[len(my_list) - 1]) # 👉️ 'c'

If you need to count the items in a list that satisfy a condition, use a list comprehension to get a subset of items that match the condition and pass the results to the len() function.

main.py
my_list = [1, 2, 3, 4, 5] result = len([x for x in my_list if x > 3]) print(result) # 👉️ 2

The example gets the count of list items that are greater than 3.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The len() function is always guaranteed to return the correct length of the list, regardless if you add or remove items.

main.py
my_list = [1, 2, 3] my_list.append(4) print(my_list) # 👉️ [1, 2, 3, 4] print(len(my_list)) # 👉️ 4 my_list.pop() print(my_list) # 👉️ [1, 2, 3] print(len(my_list)) # 👉️ 3

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 ran.

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')
Use the search field on my Home Page to filter through my more than 1,000 articles.