In this article we will discuss different ways to count number of elements in a flat list, lists of lists or nested lists.

Count elements in a flat list

Suppose we have a list i.e.

To count the elements in this list, we have different ways. Let’s explore them,

Use len() function to get the size of a list

Python provides a inbuilt function to get the size of a sequence i.e.

Arguments:

  • s : A sequence like object like, list, string, bytes, tuple etc.

It returns the length of object i.e. count of elements in the object.

Now let’s use this len() function to get the size of a list i.e.

Output:

How does len() function works ?

When len(s) function is called, it internally calls the __len__() function of the passed object s. Default sequential containers like list, tuple & string has implementation of __len__() function, that returns the count of elements in that sequence.

So, in our case we passed the list object to the len() function. Which internally called the __len__() of the list object, to fetch the count of elements in list.

Use list.__len__() to count elements in a list

We can directly call the __len__() member function of list to get the size of list i.e.

Output:

Although we got the size of list using __len__() function. It is not a recommended way, we should always prefer len() to get the size of list.

Count elements in list of lists

Suppose we have a list of list i.e.

Now we want to count all the elements in the list i.e. total numbers in list.

But if we call the len() function on the lists of list i.e.

Output

In case of list of lists, len() returns the number of lists in the main list i.e. 3. But we want to count the total number of elements in the list including these three lists. Let’s see how to do that.

Use for loop to count elements in list of lists

Iterate over the list, add size of all internal lists using len() i.e.

Output:

Use List comprehension to count elements in list of lists

Iterate over the list of lists using List comprehension. Build a new list of sizes of internal lists. Then pass the list to sum() to get total number of elements in list of lists i.e.

Output:

Count elements in a nested list

Suppose we have a nested list i.e. a list that contain elements & other lists. Also these internal lists might contain other lists i.e.

Now how to calculate the count of number of elements in this kind of nested list ?

For this we have created a recursive function that will use the recursion to go inside this nested list and calculate the total number of elements in it i.e.

Now let’s use this function to count elements in a nested list i.e.

Output

It will iterate over elements in list and for each element it will check if its type is list, then it will again call this function to get the size else returns 1.

Complete example is as follows,

Output:

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.