Register Login

Get Number of Elements in a List in Python

Updated Jan 17, 2023

Like an array, a list is a data structure that is mutable or changeable and stores elements in an ordered sequence. Users can term each element or value as "items." A string that contains characters and users write them within quotes.

So in simple words, users can define a list like a string that has elements inside square brackets "[ ]" and separated by commas [,]. This tutorial will discuss how users can get the number of elements in a list in Python using the different functions and methods.

What are lists in Python?

Users can create a list by adding different elements inside square brackets. A list can have items of distinct data types and any number of items. Also, users can add a list as an item inside another list called the nested list. Python lists are simply like dynamically sized arrays, having items with different data types.

Users are free to use any of the different methods in Python to find the number of elements of a list. The methods may differ based on whether users like to count all the lists within the main list as a single element, find the number of elements in the nested lists, or whether they need to count all the unique elements.

The functions like len(), length_hint, and a for loop will let users get the count of the number of elements in a list.

Example of the list is as follows:

a = [1, 297.5, "Hello", False]
# Here, we are printing the list
print(a)

Output:

Different Methods to obtain the number of items in a List:

  1. Using the len() function
  2. Using the length_hint function
  3. Using the for loop
  4. Number of Items in a List Containing Other Lists
  5. Using the list of a list
  6. Unique Elements in a List

Method 1: Using the len() function to get the number of items:

The built-in len() function will allow Python users to get the count of the items in a list.

Code Snippet:

# Here, we are declaring the list of items
my_list = ["Hey", 2, True, 3954.65, "Dragon", 3434]
# Here, we are printing the list
print(my_list)
# use the len() function to obtain the number of elements
print("The number of elements in the list are:", len(my_list))

Output:

Explanation:

The len() function returns the number of items present in the list.

Method 2: Using the length_hint function to obtain the number of items:

Another way of getting the number of elements of a list is using the length_hint function of Python.

Code Snippet:

from operator import length_hint
list = ["Hey", 2, True, 3954.65, "Dragon", 3434]
print(length_hint(list))

Output:

Explanation:

In the console, we can see that the function returns the total number of elements the list contains.

Method 3: Using the for loop to obtain the number of items:

Another technique with which users can do this is to make a function that loops through the list using a for loop. First, users need to initialize the count of the items to zero, and every time, the compiler will iterate through these items in the list, i.e., a loop iteration will execute, and the count increases by 1.

The loop execution will end when it finishes iterating over all the elements. Finally, the output will represent the total number of items present in the list:

Code Snippet:

# Here, we are declaring the list of items
list = ["Hey", 2, True, 3954.65, "Dragon", 3434]
# Here, we are declaring a counter variable
a = 0
# The for loop will iterate on items and keep incrementing the value of the count
for i in list:
 # It will increment the count value
 a = a+1
# Here, we will print the items on the list 
print(list)
print("The total number of items in the list are:", a)

Output:

Explanation:

The for loop will iterate over all the items in the list and finally returns the total count. But using a for loop to get the number of elements is a much more rambling solution than the len() function.

Method 4: Get the Number of Items in a List Containing Other Lists:

This method will help users count the number of items in the nested list and the main list. Here, users will use the for loop that initializes the count variable to zero and loops through the items.

Code Snippet:

a = [[34, 5, 23, 74], [234.4, 234.56, ""], [], [10, 20], "char", [True, "Hello", 69], ""]
def get_all_elements_in_list_of_lists(list):
    i = 0
    for ele in a:
        i += len(ele)
    return i
print("Total number of elements in the list of lists: ", get_all_elements_in_list_of_lists(a))

Output:

Explanation:

This method brings all the nested list items, including the number of characters inside the string. We can see that each character of the "Python" string counts towards the total number of items because the len() function operates on the string by returning all the characters.

Again, we can see that the empty list did not affect the total count. It is because in every Python for loop, users consider the length of the existing nested list. Since the length of the empty list is zero, the length count will increase by zero.

Method 5: Using the list of a list to obtain the number of items:

Users can also use the built-in len() function to count the number of the nested list inside a list.

Code Snippet:

a = [[34, 5, 23, 74], [234.4, 234.56, ""], [], [10, 20], [True, "Hello", 69], "Python"]
total = len(a)
print("The total number of elements in the list of lists: ", total)

Output:

Explanation:

The len() function will count the nested list and the string as a single element. It returns the final count of list items.

Method 6: Get the Number of Unique Elements in a List:

Often, users can see that they have added multiple items inside a list where there are duplicate items. If users want to get the number of elements without counting the same items, they can use this set() function, which rejects all the duplicate list items during its count.  

Code Snippet:

list = [1, 4358.43, "", True, 2, 1, "Hello", True]
ele = len(list)
ele1 = len(set(list))
print("The number of elements in the list: ", ele)
print("The number of unique elements in the list: ", ele1)

Output:

Explanation:

The set() function does not need any parameter, and we can see in the console that the list contains eight items and five unique items.

Conclusion:

We hope this article has discussed different ways to get the total count of items in a list and the nested lists.

Nested lists are those lists that are items or elements of another list. A Python program can have multiple lists, one inside the other. So, according to the requirement, users can use the above methods to get the number of elements of a list.


×