def dequefilter(deck, condition):
    for _ in range(len(deck)):
        item = deck.popleft()
if condition(item):
    deck.append(item)
deck = deque((1, 2, 3, 4, 5))
dequefilter(deck, lambda x: x > 2)
print(deck)
How to modify python collections by filtering in-place?