I need to create a function that takes two arguments: a list lst and a number num. If an element occurs in lst more than num times, remove the extra occurrence(s) and return the result.
So far I have:
def delete_occurrences(lst, num):
for x in lst:
if lst.count(x) > num:
lst.pop(lst.index(x, lst.index(x)+1))
return lst
However for an case such as ([1, 1, 3, 3, 7, 2, 2, 2, 2], 3) it doesn't delete the correct repetitions.