Count Total Number of Elements in List in Python (4 Examples)

 

In this tutorial, you’ll learn how to count the total number of elements in a list in the Python programming language.

The content of the tutorial looks as follows:

Let’s dive right into the examples!

 

Creation of Example Data

The first step in this tutorial is to create an example list.

my_list = ['blue', 3, 5, 'orange', 
           4, 'green', 9, 15, 33]
print(my_list)
# ['blue', 3, 5, 'orange', 
# 4, 'green', 9, 15, 33]

Have a look at the previous output. Our data is a list object that contains nine elements in total. Let’s see how to count the number of elements in my_list!

 

Example 1: Count Number of Elements in List Using len() Function

This example uses the len() function to extract the number of elements in a list.

len(my_list)
# 9

Easy! The output shows the total number of elements, which is nine many, in our list.

 

Example 2: Count Number of Elements in List Using sum() Function

It’s also possible to count the total number of elements in a list using the sum() function. Let’s take a look at the code below!

sum(1 for e in my_list)
# 9

As shown, the sum() function adds 1s as many as the number of elements in my_list via iterating through my_list. The result is nine, just as expected!

 

Example 3: Count Number of Elements in List Using For Loop

Alternatively, we can also use a for loop to calculate the total number of elements in a list. Please check the code below.

elements = 0
for e in my_list:
    elements = elements+1
print(elements)
# 9

As seen in the previous output, we iterate through the elements of our list via a for loop to get the total number of elements in it. Again, the correct result: nine is obtained.

 

Example 4: Count Number of Elements in List Using length_hint() Method

The length_hint() method of the operator module can also be handy in this case. Let’s import it first!

from operator import length_hint

Now, we can call length_hint() to get the total number of elements in my_list.

length_hint(my_list)
# 9

As supposed, the total number of elements is concluded to be nine.

 

Video, Further Resources & Summary

Do you need more explanations on how to count the total number of elements in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to count the total number of elements in a list in Python. If you have any further questions, don’t hesitate to let me know in the comments section below.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top