How to Get the Number of Elements in a Python List?
In this article, we will discuss how we get the number of elements in a Python list.
Example
Input: [1,2,3,4,5]
Output: 5
Explanation: No of elements in the list are 5Input: [1.2 ,4.5, 2.2]
Output: 3
Explanation: No of elements in the list are 3Input: [“apple”, “banana”, “mangoe”]
Output: 3
Explanation: No of elements in the list are 3
Before getting the count of items in the Python List, we have to create an empty List and store some items in the List.
Methods to Get the Number of Elements in the List
- Using Python len( ) function
- Using for loop
- Using operator length_hint function
- Using Numpy Library
Using Len() function to Get the Number of Elements
We can use the len( ) function to return the number of elements present in the list. To efficiently count items in a list, you can use Python’s built-in functions.
Python3
elem_list = [ 1 , 2 , 3 , 4 ] print (elem_list) print ( "No of elements in list are:" , len (elem_list)) |
Output:
[1, 2, 3, 4]
No of elements in list are: 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Using for loop Get the Number of Elements
We can declare a counter variable to count the number of elements in the list using a for loop and print the counter after the loop in Python gets terminated. In this way we get number of items in a list.
Python3
item_list = [ 1 , 2 , 3 , 4 ] count = 0 for i in item_list: count = count + 1 print (item_list) print ( "No of elements in the list are:" , count) |
Output:
[1, 2, 3, 4]
No of elements in the list are: 4
Time Complexity: O(n)
Auxiliary Space: O(1)
Using length_hint Get the Number of Elements in a List
In this example we are using the length_hint() function to get number of items in a list.
Python3
from operator import length_hint l = [ 1 , 2 , 3 , 4 ] print (length_hint(l)) |
Output:
4
Time Complexity: O(1)
Auxiliary Space: O(1)
Counting elements Using Numpy Library
This code uses the NumPy library to count the number of elements in a Python list named elem_list
, which contains the elements [1, 2, 3, 4]. The code prints the list and then outputs the message “No of elements in list are: 4,” indicating that there are four elements in the list.
Python3
import numpy as np elem_list = [ 1 , 2 , 3 , 4 ] print (elem_list) print ( "No of elements in list are:" , np.size(elem_list)) |
Output:
[1, 2, 3, 4]
No of elements in list are: 4
Time Complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(1)
Get Number of Unique Elements in a List
To get the number of unique elements in a list, we can use the set
data structure in Python.
In this code, my_list
contains duplicate elements. By converting it to a set, we eliminate duplicates, and then we calculate the length of the set to get the count of unique elements.
Python3
my_list = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 ] unique_elements = set (my_list) count_unique = len (unique_elements) print ( "Original List:" , my_list) print ( "Number of Unique Elements:" , count_unique) |
Output
Original List: [1, 2, 2, 3, 4, 4, 5]
Number of Unique Elements: 5
Get Total Number of Elements in a List Containing Other Lists
To get the total number of elements in a list that contains other lists, you can use recursion to traverse all the nested lists and count their elements. In this way we count items in list.
This code defines a count_total_elements
function that recursively iterates through the list and its sublists. It counts the elements and returns the total count. The example nested_list
demonstrates the function’s usage.
Python3
def count_total_elements(my_list): total_elements = 0 for item in my_list: if isinstance (item, list ): total_elements + = count_total_elements(item) else : total_elements + = 1 return total_elements nested_list = [ 1 , [ 2 , 3 , [ 4 , 5 ]], [ 6 , 7 ], 8 ] total_elements = count_total_elements(nested_list) print ( "Nested List:" , nested_list) print ( "Total Number of Elements:" , total_elements) |
Output
Nested List: [1, [2, 3, [4, 5]], [6, 7], 8]
Total Number of Elements: 8
Contact Us