What ways are there to count the occurrences of an item in a list?

Question

In this code challenge, we first need to obtain the number of occurrences of an item in a list. What ways are there to do this?

Answer

There are a few ways to count the occurrences of an item in a list. A few ways are as follows.

One way is to utilize the built-in .count() method, which applies to a list, and takes in a value as its argument. It will return the number of times the value appears in the list. The syntax is as follows,

list.count(item)

Another way to do this is using a loop. You can first start by initializing a variable to 0, which will keep track of the number of occurrences. The loop can iterate over each element of that list, and for each element, you can use an if statement to check whether that current element matches the value you are counting. If there is a match, increment the variable by 1.

The implementation of the loop will not be shown here and will be left as a challenge for you to implement. Try to solve this exercise without using the .count() method at first.

6 Likes

This seems way more elaborate and counter-intuitive than just using .count(), are there any benefits to doing it this way, instead?

1 Like

.count() is language specific function. This means that it is possible that for example javascript, c++, etc does not have this function.

In these cases you can create a count function for yourself. This is done with the loop @jephos249 is talking about.

.count() however does pretty much the same thing as the loop. The only difference is that python already created the loop for you and made it accessible with the function count().

No there are no benefits. It’s just good to know when you don’t have access to the function .count() or when the default count function does not specifically do what you want it to do.

3 Likes

I used this code and it worked:

def more_than_n(lst,item,n):
    count=0
    for i in lst:
        if i==item:
            count=count+1
        else:
            continue
    if count>n:
        return True
    else:
        return False

Hello, @denka_v.

Welcome to the forum.

I have a couple of suggestions in the form of questions.

Do you need the else included in your first if ... else?

Is there a more efficient way to return a bool value? For example what does return 5 < 4 return?

1 Like

You are right. I don’t need it there. Is the correct code? :

def more_than_n(lst,item,n):
    count=0
    for i in lst:
        if i==item:
            count=count+1
    if count>n:
        return True
    else:
        return False

Sure. That’s better. Now in your second if ... else, do you need it at all to return either True or False?
What happens when a line such as this is executed: return count > n?

1 Like

That’s right. It also returns True or False without any other terms. Thank you!

1 Like

I tried the loop but got "False"


In [206]: def more_than_n(lst, item, n): 
     ...:     item = 0 
     ...:     for item in lst: 
     ...:         item = item+1 
     ...:     if item > n: 
     ...:         return True 
     ...:     else: 
     ...:         return False 
     ...:                                                                                                                         

In [207]: print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))                                                                      
False

Can you please point out my mistake?

Hello, @sodiqolabodeafolayan.

Looks like you’ve used the name, item, a few times too often. In your code, 2 is passed to your function as the argument to be assigned to your item parameter. However, you immediately reassign item the value 0.

Then in your for statement you use item as your iterator variable. First iteration: item is assigned the first value in lst, so 2. You then use this: item = item+1, so item is now assigned 3. This process continues until you’ve iterated the entire lst, assigning item a value of 3.

Since 3 is not greater than n which is also assigned 3 from the argument passed in at the function call, False is returned.

That’s what is happening, so how do you fix it? You want to count how many times item appears in lst, and then compare that count to n returning True if the count is greater than n, and False otherwise.

1 Like

Thank you soooo much

1 Like

You can try this as well

def more_than_n(lst, item, n):
  total = lst.count(item)
  if total > n:
    return True
  else:
    return False

or this (a shorter version)

def more_than_n(lst, item, n):
  total = lst.count(item)
  return total > n
1 Like

or just:

return lst.count(item) > n

i don’t understand it why we add lst.count(item) or from where it come ?

The instructions are:

Create a function named more_than_n that has three parameters named lst , item , and n .

The function should return True if item appears in the list more than n times. The function should return False otherwise.

Well, lst, item and n are all given as parameters of the function.

lst.count(item) returns the number of times that item appears in the list, which is exactly what we need to know in order to decide

if item appears in the list more than n times.

def more_than_n(lst, item, n):
if lst.count(item) > n:
return True
else:
return False

pretty straight forward but it worked