List.count() method in Practice Problems

Hi there! Can someone explain the answer at the Conditional Statements Practice problems, screen 17/22, as i am not familiar with the method (i did not found it the first lessons or maybe i am dizzy) and i couldn`t solve it myself :frowning: i really wish to understand it.

Thank you!!

Hereโ€™s an article I found that may help you out:

Good luck!

Hi! Thanks for your reply!

Unfortunately this doesn`t help, because:

As per Programiz, The count() method returns the number of times the specified element appears in the list.
In this practice problem, we have to find the number that appears the most, so the element is unknown.

DQ gives us this answer to our practice problem:
Given a list of numbers, find the number that appears the most.
values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 32, 10, 62, 48, 32, 96, 75, 15]
most_frequent = values[0]
for value in values:
if values.count(value) > values.count(most_frequent):
most_frequent = value
print (most_frequent)

Can you explain this code to me? or someone else? Thank you!

Sorry, this was the code:

values = [72, 50, 48, 50, 7, 66, 62, 32, 33, 75, 30, 85, 6, 85, 82, 88, 30, 32, 78, 39, 57, 96, 45, 57, 61, 32, 10, 62, 48, 32, 96, 75, 15]
most_frequent = values[0]
for value in values:
    if values.count(value) > values.count(most_frequent):
        most_frequent = value
print(most_frequent)

hi @H_elen

Basically what the code does is this

  1. we take the first element of the list and save it on a variable (most_frequent)
  2. We make a for loop so we can test each value
  3. In a conditional statement we compare, using the List.count() method the number of times the current element (value) appears against the number of times the first value (most_frequent) appears
    3.1 If the current element or to be more clear the element in the list thatโ€™s being tested is bigger than the most_frequent value we save the current element as the most_frequent
  4. The loop is going to test all the elements and will find the most frequent one

hope this helps you to understand

1 Like

Hi Alejandro! Thanks a lot for your help, i got it now!! One :beer: for u! :smiley: