FAQ: Code Challenge: Lists - More Than N


#1

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!


#2

#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))


#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


#5

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