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 (
) below!
Agree with a comment or answer? 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!
catower
Split this topic
2
A post was split to a new topic: My solution
catower
Split this topic
3
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)
catower
Split this topic
7
catower
Split this topic
9
catower
Split this topic
11
3 posts were split to a new topic: Do I need to count n
?
catower
Split this topic
16
catower
Split this topic
17
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
flobaba
19
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.
mtf
20
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
flobaba
21
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
mtf
22
You’re welcome. Good work. Mark NB in your notes.
I am not sure what this problem is asking for and what does the print statement numbers mean?