Code challenge list- more than N

can someone explain the line if lst.count(item) > n? lst is also a parameter not sure why its in front of .count()

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.

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

There are some concepts that don’t come up until later, namely ‘classes’. Classes are the definitions we give to objects of a given group. For instance, list is a class that denotes the ‘type’ of object. In addition to the definition, the class also houses a number of possible ‘methods’ that act like functions specifically devoted to instances of the class.

That’s a mouthful; consider:

lst = []    #  declare an instance of a list object
print (type(lst))
# <class 'list'>

A method is a function, for the most part, but it is only available in the context of the object instance.



Not all classes have a ‘count’ attribute. If the object defined by the class is iterable, it will most likely have a ‘count’ attribute; i.e., a, .count() method.

lst above inherits the ‘count’ attribute from its class definition. We invoke it on the instance (the list object) in the same way we would poll an attribute of the object, as illustrated above.

Now it’s important to note that the parameters are not all of the same class.

  • lst is assumed to be a list type of object
  • item is assumed to be an object of the same type as are in the list
  • n is assumed to be a counting number; that is, an int object (integer).

Parameters are not themselves objects, but the local names applied to the objects given in the arguments of the function call. Those arguments are themselves variables that refer to objects in that (the calling) scope.

#     parameter
#         \
def func(param):
 return param

var = 'reference to object in memory'

print (func(var))
#           /
#       argument
1 Like