This tutorial will show a naive approach and the ‘pythonic’ way to get the length of a list in Python.

A list in Python is an ordered collection of items that can be modified. New items can be added, and existing items can be changed or removed.

Sometimes we also need to count the number of items found in a list. Another way of saying this is we sometimes need a way to get the length of a list.

Naive Way: How to get the length of a list in Python?

We can directly count the number of items in a list by using a for-loop. The next example shows how we can do this.


fish_types = ['salmon', 'trout', 'mahi mahi', 'herring', 'cod', 'mackerel', 'perch']

count = 0

for fish in fish_types:
    # increment the fish count
    count += 1

print(f"There are {count} fish types in the list below:\n{fish_types}")

The above code simply iterates over the list fish_types, and keeps track of the number of iterations by incrementing a counter variable. The program produces the following output when we run it:

Output:

There are 7 fish types in the list below:
['salmon', 'trout', 'mahi mahi', 'herring', 'cod', 'mackerel', 'perch']

The count variable accurately reflects the number of fish types in the list. We can also use a for-loop to count the number of items in a list containing lists.

When the following program is executed, the for-loop iterates over the contents of the fish_varieties list, and increments count each time.


fish_varieties = [['atlantic salmon', 'pink salmon', 'coho salmon'],
                    ['lake trout', 'brook trout'],
                    ['atlantic herring', 'pacific herring'],
                    ['atlantic cod', 'haddock', 'pacific cod', 'alaska pollock']]

count = 0

for variety in fish_varieties:
    # increment the variety count
    count += 1

print(f"There are {count} fish varieties in the list below:\n{fish_varieties}")

The output shows that the algorithm still counts the number of items in the list.

Output:

There are 4 fish varieties in the list below:
[['atlantic salmon', 'pink salmon', 'coho salmon'], ['lake trout', 'brook trout'], ['atlantic herring', 'pacific herring'], ['atlantic cod', 'haddock', 'pacific cod', 'alaska pollock']]

The for-loop only iterates over the list, fish_varieties, without iterating over the nested lists. That is why the program still produced the correct count.

Get length of list using len() function

Python has a built-in function, len(), that can help us get the number of items in a list. The len() function accepts a single argument, the list, and returns an integer representing the number of items in the list.

The following code shows how to find the length of a list by using len().


fish_types = ['salmon', 'trout', 'mahi mahi', 'herring', 'cod', 'mackerel', 'perch']

print(f"There are {len(fish_types)} fish types in the list below:\n{fish_types}")

Output:

There are 7 fish types in the list below:
['salmon', 'trout', 'mahi mahi', 'herring', 'cod', 'mackerel', 'perch']

Function len() still returns the number of items in a list without counting the number of items in any nested list. The next example shows this in action


fish_varieties = [['atlantic salmon', 'pink salmon', 'coho salmon'],
                    ['lake trout', 'brook trout'],
                    ['atlantic herring', 'pacific herring'],
                    ['atlantic cod', 'haddock', 'pacific cod', 'alaska pollock']]

print(f"There are {len(fish_varieties)} fish varieties in the list below:\n{fish_varieties}")

Output:

There are 4 fish varieties in the list below:
[['atlantic salmon', 'pink salmon', 'coho salmon'], ['lake trout', 'brook trout'], ['atlantic herring', 'pacific herring'], ['atlantic cod', 'haddock', 'pacific cod', 'alaska pollock']]

Conclusion

We have seen how to get the length of a list in python by making use of two ways; the naive approach (using a for loop to count the number of items in the list), and using python’s len() function.

We hope this tutorial helped you in your programming. Thanks for reading, and check out our other posts on python programming.

You can also read about how to remove items from the list in Python.  Or If you need programming help or looking for someone who can python help then feel free to drop us a message.

Like this article? Follow us on Facebook and LinkedIn.