How to get the number of elements in a list, list of lists or nested list in Python ?
In this article we will discuss about different ways to get number of elements in a list, lists of lists or nested list Lists. As we know in python, lists are used to store multiple values in an ordered sequence inside a single variable. Each element inside the list is called an item.
Syntax : my_list = [ element1, element2, element3, .....]
where,
- elements/items are placed inside square brackets
[].
- items are separated by , symbol.
- It can contain any number of items.
- elements can be of different types i.e string, float, integer etc.
Count elements in a flat list :
To count elements in a flat list we have to use the len( )
function. This len( )
function is a built-in function that returns the number of items inside a list, tuple, or string.
Syntax : len(name_of_list)
Let’s see an implementation of it.
#Program : #list of items listElem = ['contempt', 'speech', 'notorious', 'north','south','aware','ex'] #finding the length of list length = len(listElem) print('Number of elements inside the list : ', length)
Output : Number of elements in list : 7
- How to Create an Empty List in Python? – 2 Simple Methods
- Python : Check if all Elements in a List are Same or Matches a Condition
- Python Program to Count the Number of Null elements in a List
Count elements in list of lists :
To count the number of elements inside a list of lists we can not use the len( )
function because as the function works it will return the number of lists inside the list.
Let’s see an implementation of it.
#Program : #list of items listElem = [[10,20,30],[40,50,60],[70,80,90]] #finding the length of list of lists length = len(listElem) print('Number of elements inside the list : ', length)
Output : Number of elements in list : 3
As you can see there are 9 elements inside the list, however, the function returned 3, which is the number of lists inside the list.
So to count elements we can use two methods.
Method-1 : Using for loop to iterate over the list and count the elements :
#Program : #list of lists listElem2D = [[10,20,30],[40,50,60],[70,80,90]] count = 0 #counting number of elements for listElem in listElem2D: count += len(listElem) print('Total Number of elements : ', count)
Output : Total Number of elements : 9
Method-2 : Using list comprehension :
Using list comprehension
to iterate over the list, build a new list of sizes and calculate the number of elements by passing it to the built-in sum( )
function.
#Program : #list of lists listElem2D = [[10,20,30],[40,50,60],[70,80,90]] #finding number of elements count = sum( [ len(listElem) for listElem in listElem2D]) print('Total Number of elements : ', count)
Output : Total Number of elements : 9
Use list.__len__() to count elements in a list :
We can call the __len__()
member function of list to get the size the list.
#Program : #list of items listElem = [10,20,30,40,50] #finding the length of list of lists length = listElem.__len__() print('Number of elements inside the list : ', length)
Output : Number of elements inside the list : 5
Count elements in a nested list :
Nested lists might contain several lists, lists of lists, or values together so we can not use any of the above approaches for it. So to calculate the values we have to write a function that takes the nested list and returns the number of items inside it.
Let’s see an implementation of it.
#Program : def getSizeOfNestedList(listOfElem): count = 0 for elem in listOfElem: if type(elem) == list: count += getSizeOfNestedList(elem) else: count += 1 return count #list of lists listElemNested = [[10,20,30],[40,50,60],[70,80,90],100,120,140,[[110,130],150],170] count = getSizeOfNestedList(listElemNested) print('Total Number of elements : ', count)
Output : Total Number of elements : 16