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

So this is how this challenge was worded: “The function should return True if item appears in the list more than n times. The function should return False otherwise.”

And in the problem/challenge that immediately follows it we’re asked to, " Return either item1 or item2 depending on which item appears more often in lst ."

For me, the wording in the first set of instructions was a little confusing because I felt that I needed to make the assumption that when they referred to “list” they meant “lst,” which I didn’t. So, I kept using, more_than_n.count(item), which is wrong but I guess I was treating more_than_n as a list itself instead of the function that it is.

I don’t know if anyone else made the same mistake or was as easily confused as I was. Just wanted to share. I’m open to any feedback or correction on my mistake

Is it just me or are there a lot of inconsistencies with this place? I use the following:

#Write your function here
def more_than_n(lst, item, n):
if lst.count(item) > n:
return True
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))

get the error:
more_than_n([2, 3, 4], 2, 1) should have returned False , and it returned None

Drop the same code into Pycharm and I get:

True

This is not hard, and works perfectly fine on my local python install. Not the first issue I’ve had running through these exercises, especially as they start to get a little more complex. But getting very frustrating trying to figure out what the online interpreter is looking for, and finding work-arounds for it to pass. Only about 30% through the first course and this is looking to be a complete waste of money.