Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Find all elements count in list

Python | Find all elements count in list

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




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


Output

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is : 9

Time Complexity: O(n*m) where n is the number of sublists in the test_list and m is the length of the sublists.
Auxiliary Space: O(1)

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




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


Output

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is : 9

Time complexity: O(mn), where m is the number of sublists in the original list and n is the length of each sublist. This is because the chain function takes O(mn) time to flatten the sublists, and the len function takes O(1) time to calculate the length of the resulting list.
Auxiliary space: O(mn), as it requires a list of size mn to store the result of the chain function.

Method #3: Using reduce() The reduce() function from the functools library can be used to apply a function to the elements of a list, reducing it to a single value. In this case, we can use it along with the len() function to add up the lengths of the nested sublists. Here is an example of how to use the reduce() function to find the total number of elements in a list including those in nested lists:

Python3




from functools import reduce
 
# Initialize list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# Print original list
print("The original list:", test_list)
 
# Use reduce() function along with len() function to add up the lengths of the nested sublists
total_count = reduce(lambda x, y: x + len(y), test_list, 0)
 
# Print total count
print("The total element count in lists is:", total_count)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list: [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is: 9

In terms of time complexity, the reduce() function has a linear time complexity, O(n), as it processes each element in the list once. In terms of space complexity, the reduce() function has a constant space complexity, O(1), as it only stores the accumulator value and the current element being processed in memory.

Method #4 : Using extend() and len() methods

Python3




# Python3 code to demonstrate
# count of all the elements in list
 
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
 
# count of all the elements in list
res = []
for i in test_list:
    res.extend(i)
# print result
print("The total element count in lists is : " + str(len(res)))


Output

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is : 9

Time Complexity : O(N)
Auxiliary Space : O(1)

Method #5: Using itertools.flatten() function

we can use the itertools module’s chain.from_iterable() function to flatten the list and then use len() to count the number of elements. 

Step 1: Import the itertools module.

Step 2: Use the chain.from_iterable() function to flatten the list.

Step 3: Use the len() function to count the number of elements.

Python3




import itertools
 
# initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# printing original list
print("The original list : " + str(test_list))
 
# count of all the elements in list using itertools.flatten()
res = list(itertools.chain.from_iterable(test_list))
print("The total element count in lists is : " + str(len(res)))


Output

The original list : [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is : 9

Time complexity: O(n), where n is the total number of elements in the list.

Auxiliary space: O(n), where n is the total number of elements in the list. 

Using numpy:

Algorithm:

Initialize a list of lists, e.g. test_list.
Import the numpy library, e.g. import numpy as np.
Use numpy’s concatenate() function to flatten the list and get the total count of elements, e.g. np.concatenate(test_list).size.
Print the original list and the total element count in the lists, e.g. print(“The original list: “, test_list) and print(“The total element count in lists is: “, res).

Python




import numpy as np
 
# Initializing list
test_list = [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
 
# Printing the original list
print("The original list: ", test_list)
 
# Using numpy's concatenate function to flatten the list and get the total count of elements
res = np.concatenate(test_list).size
 
# Printing the result
print("The total element count in lists is: ", res)


Output:
The original list:  [[1, 4, 5], [7, 3], [4], [46, 7, 3]]
The total element count in lists is:  9

Time Complexity: O(n*m), where n is the number of sublists in the test_list and m is the length of the largest sublist, because the concatenate function is used to flatten the list, and it takes time proportional to the size of the resulting array, which is the sum of the sizes of all the sublists.

Auxiliary Space: O(n*m), because the concatenate function creates a new array to store the flattened list.

RELATED ARTICLES

Most Popular

Recent Comments