Getting the number of elements in a list in Python is a common operation. For example, you will need to know how many elements the list has whenever you iterate through it. Remember that lists can have a combination of integers, floats, strings, booleans, other lists, etc. as their elements: Show
If we count the elements in 5 we get 5 elements overall. If we do the same for 6 we will get 4 elements.There are different ways to get the number of elements in a list. 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 unique elements, and similar. Built-in Function len()The most straightforward way to get the number of elements in a list is to use the Python built-in function 7.Let's look at the following example:
Which prints out:
As the name function suggests, 7 returns the length of the list, regardless of the types of elements in it.Using a for LoopAnother way we can do this is to create a function that loops through the list using a 9 loop. We first initialize the count of the elements to 0 and every time a loop iteration is performed, the count increases by 1.The loop ends when it iterates over all the elements, therefore the count will represent the total number of elements in the list:
Running this code will print:
This is a much more verbose solution compared to the 7 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. Additionally, you might want to perform some operation either on the elements themselves or an operation in general, which is possible here.Get Number of Unique Elements in a ListLists can have multiple elements, including duplicates. If we want to get the number of elements without duplicates (unique elements) we can use another built-in function 1. This function creates a 2 object, which rejects all duplicate values.We then pass that into the 7 function to get the number of elements in the 2:
Which prints:
We can see that 5 has a total of 8 elements, among which 5 are unique.List of Lists using len()In the introduction, we saw that elements of lists can be of different data types. However, lists can have, in turn, lists as their elements. For example:
If we use the built-in function 7, the lists count as single elements, so we will have:Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Which prints:
Note that the empty list counts as one element. If a list within a list contains more than one element, they aren't taken into consideration. This is where a 9 loop comes in handy.Get Number of Elements in a List Containing Other ListsIf we want to count all the elements inside a list containing other lists, we can use a 9 loop. We can initialize the 9 variable to 0 and loop through the list. In every loop iteration, 9 increases by the length of that list.We will use the built-in function 7 to get the length:
The output is: 0There are a few important things to note in this example. Firstly, this time the empty list did not affect the total count. 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, 9 is increased by 0.However, you can see that every character of the string 3 counts towards the total number of elements. This is because the 7 function acts on the string by returning all its characters. We can avoid this situation by using the same approach as in the section below, which would also allow us to have elements other than lists.Another fun way of doing the same thing as in the previous example is by using list comprehension: 1This line essentially does two things. First, it creates a new list containing the lengths of all the elements of the original list. In our case that would be 5. Secondly, it calls the 6 function using the newly generated list as a parameter, which returns the total sum of all the elements, giving us the desired result.Nested ListsNested lists are lists that are elements of other lists. There can be multiple levels of lists inside one another: 2We can see that 7 is contained in the list 8, which, in turn, is contained in the main list 9.Again, we initialize the 9 variable to 0. If we want to get the overall number of elements in the nested list, we first need to check if the element is a list or not. If it is, we loop inside the list and recursively call the function until there are no nested lists left. All the elements other than lists (integers, strings, etc.) will increase the count by 1.Note that this is also the solution to the problems caused by the previous approach. Let's take a look at the code for counting elements in nested lists: 3Running this code would give us: 4Note that we used the built-in function 1 that checks if the first argument is an instance of the class given as the second argument. In the function above, it checks if the element is a list.The first element 2 is an integer, so the function jumps to the 3 block and increases the count by 1. When we get to 4, the function recognizes a list, and recursively goes through it to check for other lists.ConclusionWe saw that according to the type of list we have, there are different ways to get the number of elements. 7 is definitely the quickest and simplest function if we have flat lists.With lists of lists and nested lists, 7 will not count the elements inside the lists. In order to do that, we need to loop through the whole list.
How do you use the list count in Python?The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.
What is function of count for list?The COUNT function counts the number of cells that contain numbers, and counts numbers within the list of arguments. Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers.
Why count is used in Python?Python String count()
The count() method returns the number of occurrences of a substring in the given string.
What is count and index in Python?The index method requires one argument, and, like the count method, it takes only strings when index is used on strings, and any type when it is used on lists. For both strings and lists, index returns the leftmost index where the argument is found.
|