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!

#Write your function here
def more_than_n(lst,item,n):

Create a function named more_than_n that has three parameters

named lst, item, and n.

if lst.count(item) > n:
return True

Function should return True if item appears in the list more than

n times. This means the lst, the one that has all the numbers,

need to be checked the number of times from the item

therefore, lst needs to attach itself to ‘.count’, to count (duh)

all of the ‘items’ and to see if its more than n.

else:
return False

#Uncomment the line below when your function is done
print(more_than_n([2, 4, 6, 2, 3, 2, 1, 2], 2, 3))

I found the solution with my second attempt but can someone explain to me why my first attempt did not work? Cheers

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

It works for me. I just had to fix the indentation, which is important in python.

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

#Write your function here

def more_than_n(lst, item, n):
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.

my code works but the message still says it is wrong.

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

it does return True, so I don’t see what the problem is ???

True and False are values, not strings.

As an analogy, consider:

print(5 + 7 == '12')
print(5 + 7 == 12)

Output:

False
True

Why don’t I have to use .count() on the —n---- value?

Because it is a value, not an iterable, so has nothing to count. The purpose here is to determine if the item count is greater than N.

m = lst.count(item)
return m > n

Thank you mtf! That helps a lot it’s those little things I’m trying to understand. I did a bunch of research online too just couldn’t find exactly what I needed.

1 Like