I cannot understand why iterator skips values in my function. Could someone help me with that? If it is needed the task is to create a new list that contains each number of list at most N times, without reordering.
from itertools import repeat
def delete_nth(order, max_e):
def remove_from_list(i, list1): # function to remove specific values from existing list
if i in list1:
list1.remove(i)
return remove_from_list(i, list1)
else:
return list1
new_list = []
for x in order: # SKIPS value 7
if order.count(x) <= max_e:
new_list.extend(repeat(x, order.count(x)))
order = remove_from_list(x, order)
else:
new_list.extend(repeat(x, max_e))
remove_from_list(x, order)
return new_list
print(delete_nth([1,1,3,3,7,2,2,2,2], 3))
I tried to change a little order, then it skips some others values too, but I cannot find the connection.