0

I'm solving some katas in CodeWars and in the current kata I'm trying to get how many times an element has occurred in a list that I set as a parameter, then and use it as the range of a for loop.

However, when I call the function it returns:

File "<pyshell#8>", line 3, in delete_nth 
if order.count(item) > max_e:
    AttributeError: 'NoneType' object has no attribute 'count' 

here is my code

def delete_nth(order,max_e):
    for item in order:
        if order.count(item) > max_e:
            for i in range(order.count(item) - max_e):
                order = order.remove(item)
    return order

l = [20,37,20,21]
delete_nth(l,1) #except [20,37,21]
LiadS
  • 13
  • 1

2 Answers2

1

The problem is this statement order = order.remove(item) You don't have to reassign list after removing an item. And also list.remove returns None which the cause of your error.

So edit it to this: order.remove(item)

Naman Jain
  • 28
  • 6
0

The problem is that you are changing order even as you are iterating through it. You should not be doing it. If you want create a copy of the existing list and modify it.

remove function doesn't return anything, so, effectively None gets assigned to order.

def delete_nth(order,max_e):
    for item in order:
        if order.count(item) > max_e:
            for i in range(order.count(item) - max_e):
                order = order.remove(item) ======> order becomes None as remove returns None.
    return order
Jay
  • 24,173
  • 25
  • 93
  • 141