Getting the length of list is quite common problem and has been dealt with and discussed many times, but sometimes, we require to better it and find total number of elements, i.e. including those of nested list as well. Let’s try and get the total count and solve this particular problem.
Method #1 : Using list comprehension + len()
We can solve this problem using the list comprehension as a potential shorthand to the conventional loops that we may use to perform this particular task. We just iterate and count the nested list and at end return the cumulative count using len function.
# Python3 code to demonstrate # count of all the elements in list # Using list comprehension # initializing list test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]] # printing original list print("The original list : " + str(test_list)) # using list comprehension # count of all the elements in list res = len([ele for sub in test_list for ele in sub]) # print result print("The total element count in lists is : " + str(res)) |
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]] The total element count in lists is : 9
Method #2 : Using chain() + len()
This particular problem can also be solved using the chain function instead of list comprehension in which we use the conventional len function to check the lengths.
# Python3 code to demonstrate # count of all the elements in list # Using chain() + len() from itertools import chain # initializing list test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]] # printing original list print("The original list : " + str(test_list)) # using chain() + len() # count of all the elements in list res = len(list(chain(*test_list))) # print result print("The total element count in lists is : " + str(res)) |
The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]] The total element count in lists is : 9
Recommended Posts:
- Python - Count elements in tuple list
- Python | Count the elements in a list until an element is a Tuple
- Python | Count occurrence of all elements of list in a tuple
- Python | Find missing elements in List
- Python | Find sum of frequency of given elements in the list
- Python program to find sum of elements in list
- Python | Find elements of a list by indices
- Python program to find N largest elements from a list
- Python | Find top K frequent elements from a list of tuples
- Python | Find common elements in list of lists
- Python program to find number of m contiguous elements of a List with a given sum
- Python | Find the list elements starting with specific letter
- Python | Add list elements with a multi-list based on index
- Python | Subtract two list elements if element in first list is greater
- Python | Replace elements in second list with index of same element in first list
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python - Assigning Key values to list elements from Value list Dictionary
- Python | Maximum sum of elements of list in a list of lists
- Python - Check List elements from Dictionary List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

