FAQ: Code Challenge: Lists - More Than N

This community-built FAQ covers the “More Than N” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise More Than N

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: My solution

2 posts were split to a new topic: Is my indentation wrong?

#Write your function here

def more_than_n(lst, item, n):
return (lst.count(item) > n)

2 posts were merged into an existing topic: What ways are there to count the occurrences of an item in a list?

2 posts were split to a new topic: I printed a true string, why doesn’t it pass?

3 posts were split to a new topic: Do I need to count n?

A post was split to a new topic: How do I know what I need to count?

A post was split to a new topic: Why doesn’t this code work?

ok so can anyone explain to my why my code which was the inverse of the given solution didnt return False?

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

Hello. I tried implementing the exercise with the for loop:


def more_than_n(lst, item, n): 

  count = 0 

  for item in lst:

    count += 1

  print(count)

  if count > n:

    return True

  else:

    return False

In the middle, I put in a print command to check my count and it printed 8 which I know is the wrong count.

Could someone please point out where my error is? Thank you.

This is wiping out the item parameter, so there is nothing to count.

Think what it is we are trying to do.

  • iterate over lst
  • compare to item
  • increase count if iteration value matches
  • compare count to n
  • return boolean from comparison

def more_than_n(lst, item, n): 
  count = 0 
  for x in lst:
    if x == item:
      count = count + 1
    
  print(count)
  return count > n


Ah, gotcha. Thanks a lot Roy. I was not familiar with how the for loop is set up. Makes sense.

1 Like

You’re welcome. Good work. Mark NB in your notes.