3 Examples of Python 3 list len() function

Table of Contents

A Python list is a collection of items in a particular order. We can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in our family.

We can put anything we want into a list, and the items in our list don’t have to be related in any particular way. Because a list usually contains more than one element, it’s a good idea to make the name of our list plural, such as letters, digits, or names.

Python counts the items in a list starting with one, so we shouldn’t run into any off-by-one errors when determining the length of a list.

In this article, we will share 3 examples of finding the length of a list in Python.

Find the length of a list with len() function

The simplest example to find the length of a list is the len() function, which counts the number of items in some kind of container object, such as a dictionary or list. we’ve seen it before, demonstrated as follows::

>>> len([1,2,3,4])

>>> cars = [‘bmw’, ‘audi’, ‘toyota’, ‘subaru’]
>>> len(cars)

Len() in Python for loop

A common Python technique is to use range(len(someList)) with a for loop to iterate over the indexes of a list.

For example, enter the following into the interactive shell:

>>> supplies = [‘pens’, ‘staplers’, ‘flamethrowers’, ‘binders’]
>>> for i in range(len(supplies)):
print(‘Index ‘ + str(i) + ‘ in supplies is: ‘ + supplies[i])

Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binders

Using range(len(supplies)) in the previously shown for loop is handy because the code in the loop can access the index (as the variable i) and the value at that index (as supplies[i]).

Best of all, range(len(supplies)) will iterate through all the indexes of supplies, no matter how many items it contains.

Using enumerate function to replace range(len())

Instead of using the range(len(someList)) technique with a for loop to obtain the integer index of the items in the list, we can call the enumerate() function instead.

On each iteration of the loop, enumerate() will return two values: the index of the item in the list, and the item in the list itself.

>>> supplies = [‘pens’, ‘staplers’, ‘flamethrowers’, ‘binders’]
>>> for index, item in enumerate(supplies):
print(‘Index ‘ + str(index) + ‘ in supplies is: ‘ + item)

Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binder

Check Empty list with len function

In the following example, we will initialize an empty list, and check programmatically if the list is empty or not using len() function.

myList = []
if (len(myList) == 0):
print(‘The list is empty.’)
print(‘The list is not empty.’)

Quiz about Python List len

lst = [‘abc’, ‘def’, ‘ghi’]
lst[1] = ‘wxyz’


A. 3

B. 9

C. 10

D. 4

Answer: A. Changing a list value is allowed (because lists are mutable).

But changing the value at index 1 to a longer string doesn’t change the fact that the list has three values.


Table of Contents

Share on facebook
Share on twitter
Share on linkedin

You might also like