# 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:

- Count Min & Max Occurrences in List in Python
- Count Number of Dictionaries in List in Python
- Count Duplicates in List in Python
- Count Number of Integers in List in Python
- Introduction to Python

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.

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.

### Statistics Globe Newsletter