0

I know how to remove elements from a list while iteration. What about similar operation in deque? In the following example, 'my_deque' is a deque.

 for event in my_deque.copy(): if event in event_sets: remove event from my_deque 

Is there some way to do this? Can deque only push and pop from two ends?

1

1 Answer 1

2

I do not know if I understand your question, but if you want to remove the first occurrence of x in a deque you could use the function my_deque.remove(x)

from collections import deque my_deque = deque([1,2,3,4,5,6]) for event in my_deque.copy(): if event in [2,4,5]: my_deque.remove(event) print(my_deque) # deque([1, 3, 6]) 

I hope you could find this useful!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.