 # Does count() generate an error if it cannot find any items?

### Question

In this exercise, `count()` is used to find the number of items in a list. If the item being counted isn’t in the list, will `count()` return an error?

No, `count()` will not return an error if the item passed to the function is not found. The `count()` function will return a value of `0`. This is shown in the example code below. The first two items passed to `count()` are present in the `vehicles` list. The third item is not present in the list at all.

``````vehicles = ['car', 'truck', 'car', 'van', 'car', 'van']

# Returns value of 3
cars = vehicles.count('car')

# Returns value of 2
vans = vehicles.count('van')

# Returns value of 0
suvs = vehicles.count('suv')
``````
11 Likes

How can we count the number on the basis of an element of sublist?
For example,
List = [[“Sam”,12] , [“Ron”, 17], [“Josh”, 12]]
and we need to count how many of them are aged 12.

2 Likes

You have a variety of choices regarding how to do this. The following demonstrates the technique of unpacking the elements of inner `list`s within a `for` loop:

``````friends = [["Sam", 12], ["Ron", 17], ["Josh", 12]]
def age_count(people, age_to_count):
count = 0
# unpack people, people into name, age
for name, age in people:
if age == age_to_count:
count += 1
return count

print(age_count(friends, 12))
``````

Output:

``````2
``````

Edited on May 27, 2019 to add the following:

If you would like, instead, to use the `list.count()` method that is the subject of the exercise, you can unpack `name` and `age` within a list comprehension, then count the cases where `age` is equivalent to `12` within the resulting `list`, as follows:

``````friends = [["Sam", 12], ["Ron", 17], ["Josh", 12]]
age_12_count = [age for name, age in friends].count(12)
print(age_12_count)
``````

Output:

``2``
8 Likes

Hello appylpye, i would like your help once again.

``````
def two_euro_slices(pizzas_by_price,two_euro_pizza):
count = 0
for type_of_pizza,price in pizzas_by_price:
if price == two_euro_pizza:
count +=1
return count
``````

As you see i tried to use the piece of code you wrote first, when i needed it for an exercise in codecademy(i didn’t copy paste it, first i spend my time trying to understand how it works and then i used it.) Anyway it helped a lot!

The only thing that keeps troubling me till this moment is this:
As you see the return command is exactly below the for.
When it is typed like this everything works fine as it’s supposed to.

But, when i accidentaly moved it a little, for example:

``````def two_euro_slices(pizzas_by_price,two_euro_pizza):
count = 0
for type_of_pizza,price in pizzas_by_price:
if price == two_euro_pizza:
count +=1
return count
``````

when i put it under the count , the whole result is changing.

on the first example it returned 3, which was correct, as i was trying to count the pizza slices that cost 2 euros from a list with pizza types.

but on the second example it returned 1, and i have absolutely no clue about how this worked!

1 Like

Don’t forget what return does:

1. It returns a value to the calling statement
2. It halts processing of the function

In other words, once return is reached one time, the function halts (and actually disappears from the call stack - Python’s “active memory” - along with all of its variables.)

3 Likes

You initialized `count` to `0` here:

``````  count = 0
``````

Within the `for` loop, you have this `if` block:

``````    if price == two_euro_pizza:
count +=1
return count
``````

Considering what @patrickd314 wrote about `return` statements, what happens the first time within the `for` loop that the condition within the `if` block header evaluates to `True`? Also, what would happen if it did not evaluate to `True` during any of the loop iterations?

2 Likes

ok i think i get it, correct me if i’m wrong please.
what you are saying is that it prints “1” because once it finds that there is one slice of pizza that costs 2 euros, it stops. right?

Yes, if during any iteration of the `for` loop, the condition in the `if` block header becomes `True`, the code within the `if` block executes. Accordingly, the value of `count` increases to `1`. Then the `return` statement executes, which returns that value and terminates execution of the `two_euro_slices` function.

What happens if none of the pizza slices have a price of €2? In that case, the value `2` does not appear in the `pizzas_by_price` list at all, and the returned value should be `0`. However, the `for` loop executes all of its iterations, going through the entire list, without the `if` block condition’s ever evaluating to `True`. After the loop has completed all of its iterations, the value of `count` is still `0`, which is fine, but there is no `return` statement after the loop. Control reaches the end of the function’s statements, and by default, `None` is returned. With there being no €2 slices, `0` would be more appropriate as a returned value.

The first of the two versions of your function produces a correct result , provided that appropriate arguments are passed to it. In fact, since the name of the function reveals that it was specifically written for the purpose of counting €2 slices, the second argument would presumably always be `2`. On that basis, the second parameter, `two_euro_pizza`, should be eliminated to simplify the function, as follows:

``````def two_euro_slices(pizzas_by_price):
count = 0
for type_of_pizza, price in pizzas_by_price:
if price == 2:
count +=1
return count
``````

You could, instead, rename the original version of the function, so that it is appropriate for counting pizzas of whatever price is passed to it as the second argument.

3 Likes

If you act as the computer, and execute the code, is that what happens? Or do you encounter something that you don’t know what it does?

The answer is already in the code, right? Or if not, then there’s something important in it to learn. Either way, read it.

2 Likes

What is [age for name, age in friends] doing? I am not able to comprehend this.

In the unit on Lists and Dictionaries we cover the concept of `comprehensions` which are loop constructs contained in a data structure body. The loop above iterates over an object consisting of value pairs. The first value is given as a name, and the second as an age. Each pair is polled and the age value is appended to the list.

The resulting list will consist of only the ages, `[12, 17, 12]`.