Python count list

Python count list

In Python the count method on the list data structure is used to count the number of occurrences of an element in the main list, but not within any sublists. If the element is not found, the value will be 0.

Python count list – count the number of occurrences in list

We take as a reference a list of votes, then we apply the method count to find how many times the number 7 appears, for example. We store this value in a variable and finally we print the result.

votes = [9,7,7,10]
count_votes = votes.count (7)
print (count_votes)

In this case, the value 2 will be output, i.e. the number of times the number 7 is required.

Python count list – count an element that is not in the list

Now let’s try to count a missing element. So what will we receive in output?

votes = [9,7,7,10]
count_votes = votes.count (6)
print (count_votes)

In this case the value 0 will be output.

Try the code in the online compiler that you find at the following link: online Python compiler.

Python count list – on a list of strings

We create a list of names, then apply the count method to search for a name. Then we print the obtained value.

names = [‘Cristina’, ‘Tom’, ‘Cristina’, ‘Jessica’]
count_names = names.count (‘Cristina’)
print (count_names)

What changes is just that we have to be careful about putting quotes in the count method.

If the name does not exist, the value 0 will always be printed.

Attention, if we enter the name with a lowercase initial we will clearly not find it and 0 will be returned.

Python count list – use of the variable

We ask you to enter a name as input and then we store it in a varibial. Then we search for it using the count method.

name = input (‘Insert a name’)
names = [‘Cristina’, ‘Tom’, ‘Cristina’, ‘Jessica’]
count_names = names.count (name)
print (count_names)

We can thus enter a different name from time to time.

Conclusion

In this lesson we studied the count method on the list data structure in Python, creating very simple examples. In the next lesson we will study the index method.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot

Python len list

Python len list

In Python the len method on the list allows us to get the number of elements in a list, ultimately it indicates the length of a list in Python.

The syntax of the method is very simple:

len(list)

The function takes only one parameter, the list to calculate the length of. This parameter is mandatory.

Python len list – first example

Let’s create a practical example of using the function for calculating the length of a list in Python.

Let’s first create a list that contains the seasons:

seasons = [‘Spring’, ‘Summer’, ‘Autumn’, ‘Winter’]

Then we apply the len function on the Python list element:

len(seasons)

We can also print the value using print:

print(len(seasons))

The result will be 4.

We can also use a variable to store the result:

len_stagioni = len(seasons)

Then, later on, we will use the variable_season within our program, such as for example.

print(len(seasons))

Python len list – second example

Calculating the length of a list is very useful when we need to iterate.

For example, suppose you want to print all the elements of a list. In our case of seasons.

At first I present an incorrect approach to the problem, explaining the reasons.


seasons = ['Spring', 'Summer', 'Autumn', 'Winter']
for i in range(4):
   print(seasons[i])

In this case, the code could also be fine, because I will never add another season to the list. But think of lists that can be easily expanded by adding more elements.

To make everything work, I will have to change the parameter within the range every time! But does it seem correct to you? I would say no!

So how do I solve the algorithm? Using the len function on the list, like so:


seasons = ['Spring', 'Summer', 'Autumn', 'Winter']
for i in range(len(seasons)):
   print(seasons[i])

We can also use the variable to store the length of the list.


seasons = ['Spring', 'Summer', 'Autumn', 'Winter']
len_seasons = len(seasons )

for i in range(len_seasons):
   print(seasons[i])

The len function, for calculating the length of a list in Python, as we will see later, will be very useful when we want to populate the elements of the list without producing duplicates. Follow me in the next tutorials to find out how!

Conclusion

In developing the exercises we will develop many exercises using the len function on the Python list element.

Some useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot