

This is because in every loop we consider the length of the current nested list, and since the length of an empty list is 0, count is increased by 0. Firstly, this time the empty list did not affect the total count. There are a few important things to note in this example. The output is: Total number of elements in the list of lists: 16 Print( "Total number of elements in the list of lists: ", get_all_elements_in_list_of_lists(list_e)) We will use the built-in function len() to get the length: list_e =, ,, , "char", ]ĭef get_all_elements_in_list_of_lists( list): In every loop iteration, count increases by the length of that list. We can initialize the count variable to 0 and loop through the list. If we want to count all the elements inside a list containing other lists, we can use a for loop. Get Total Number of Elements in a List Containing Other Lists This is where a for loop can come in handy. If a list within a list contains more than one element, they aren't taken into consideration. Note that the empty list counts as one element. Which prints: Number of elements in the list of lists: 6 Print( "Number of elements in the list of lists: ", number_of_elements) If we use the built-in function len(), the lists count as single elements, so we will have:ĭownload the eBook number_of_elements = len(list_e) However, lists can have, in turn, lists as their elements. In the introduction of this article, we saw that elements of lists can be of different data types. We can see that list_d has a total of 8 elements, among which 5 are unique. Which prints: Number of elements in the list: 8 Print( "Number of unique elements in the list: ", number_of_unique_elements) Number_of_unique_elements = len( set(list_d)) We then pass that into the len() function to get the number of elements in the set: list_d = This function creates a set object, which rejects all duplicate values. If we want to get the number of elements without duplicates (unique elements) we can use another built-in function set(). Lists can have multiple elements, including duplicates. Additionally, you might want to perform some operation either on the elements themselves or another operation in general, which is possible here. This is a much more verbose solution compared to the len() function, but it is worth going through it as we will see later in the article that the same idea can be applied when we're dealing with a list of lists.
PYTHON LIST COUNT RUN TIME CODE
Running this code will print: Number of elements in the list: 6 Print( "Number of elements in the list: ", get_number_of_elements(list_c)) The loop ends when it iterates over all the elements, therefore the count will represent the total number of elements in the list: list_c = We first initialize the count of the elements to 0 and every time a loop iteration is performed, the count increases by 1.

Using a for LoopĪnother way we can do this is to create a function that loops through the list using a for loop. This method does not count the lengths of any lists within the list, only top-level elements. Which prints out: Number of elements in the list: 5Īs the name function suggests, len() returns the length of the list, regardless of the types of elements in it. Print( "Number of elements in the list: ", number_of_elements) The most straightforward and common way to get the number of elements in a list is to use the Python built-in function len(). The approaches vary whether you want to count nested lists as one element or all the elements in the nested lists, or whether you're only interested in top-level unique elements. There are different ways to get the number of elements in a list. If we do the same for list_b we will see that we have 4 elements. If we manually count the elements in list_a we get 5 elements overall. # List of integers, floats, strings, booleans as their elements: # List of just integers Remember that lists can contain a combination of types, like integers, floats, strings, booleans, other lists, etc. For example, you will need to know how many elements the list has whenever you iterate through it. Getting the number of elements in a list in Python is a common operation.
