Count elements in a list, list of lists, nested lists in Python

Python List

In this post, we are going to understand ways to Count elements in a list, list of lists, nested lists in Python. While doing the data manipulation we need to get the length of the list, list of lists, nested lists. So we need some techniques to do this kind of job for us.

When it comes to counting the number of elements in the list first idea that comes to our mind is the list. count() or else we can use len() method of the list. Let us understand with an example of how we can use both of them.Ways to get number elements in a list, list of lists, nested lists in Python

1. Using in-built len() function

The len() is an in-built method in python that takes an iterable( string, tuple, dictionary, list) as an argument and returns the integer number as the size or length of iterable.

Let us understand with an example:

animal_list = ['dog','cat','mouse','cat','bee','cat','cat','rat']
length_of_list = len(animal_list)
print('length of list is =',length_of_list)

Output

length of list is = 8

2. Get length of the list of lists

The len() built-in function of the LIST class can be used to count a number of elements in the list of lists. It does not matter what is the data types of elements in the list of lists. In the list of lists, it only gets the length of the parent list, not the elements of the list that in the parent list.

The length would be the number of elements in the parent list and the number list in the parent list. we have 5 elements in the parent list and 3 lists in it. So the length would be 8.

#program to count the number of elements in the list of lists python

list_of_lists = ['dog',[10,16],[1,5,7],['a','bc'], 'cat', 'bee', 'cat','cat']

len_list_of_lists = len(list_of_lists)

print('length of list of lists is =',len_list_of_lists)

Output

length of list of lists is = 8

2.1. Count elements in list of lists Using for-loop

As we saw above the len() method does not count elements of the list in the parent list, but we can count a total number of elements including the list in the parent list using the for a loop.

We are looping over the list of lists, for each element len() function returning length by counting number of character in string type element.

But as we can see if the elements inside the list of the list, len() method return its length by count the total number of character as taking the example of element ‘dog’ in the list so list length would be 3. so on for all elements.

#program to count the number of elements in the list of lists python

list_of_lists = ['dog',[10,16],[1,5,7],['a','bc'], 'cat', 'bee', 'cat', 'cat']

lists_elements_count = 0

for ele in list_of_lists:
    lists_elements_count += len(ele)    

print('length of list of lists is =',lists_elements_count)

Output

length of list of lists is = 22

2.2. Get length of list of lists using list comprehensions

Another simple method to do this by using lesser code is by using the list comprehensions.

Let us understand , How does it work?

Firstly create a new list consists of a length of parent and list inside parent list as per list below the new list would be [15,0,2,3,2] and pass the list and count the sum of elements of the new list it would be 22.

#program to length of list of lists in python
list_of_lists = ['dog','cat','bee','cat','cat',[],[10,16],[1,5,7],['a','bc']]
elements_count = sum( [ len(ele) for ele in list_of_lists])    

print('length of list of lists is =',elements_count)

Output

length of list of lists is = 22

3. Get count of elements in Nested list

Here, we are calling the function recursively to get the length of the nested list. The function runs recursively to check the type of element in the list and run a loop over the element of the list and return the elements count.

#program to length of nested list in python
nested_list = ['dog','cat',['bee',[10,16,[23,24]],[1,5,7],45],['a','bc'],[40,[3,4]]]


def get_nested_length(list):
    
    size = 0
    
    for item in list:
     
        if type(item) == list:  
           
            size += get_nested_length(item)
        else:
         size+=1
    return size

#calling the upper define method 
print("length of nested list is = ",get_nested_length(nested_list))

Output

length of nested list is = 5

Conclusion

In this article we learnt about the different ways to get the length of the list of lists in Python. I hope you will find these methods helpful in your applications.

Happy Learning!!