Deque

Defining a Deque

  • A queue is a collection that:

    • Is indexed
    • Allows duplicates
    • Is linear
    • Is FIFO

Creating a Deque

>>> from collections import deque
>>> fruits = deque(['apple', 'banana', 'pear'])
>>> print(fruits)
deque(['apple', 'banana', 'pear'])

Adding to a Deque

>>> from collections import deque
>>> fruits = deque(['apple', 'banana', 'pear'])
>>> fruits.append('mango')
>>> print(fruits)
dequeu(['apple', 'banana', 'pear', 'mango'])

Removing from a Deque

>>> from collections import deque
>>> fruits = deque(['apple', 'banana', 'pear'])
>>> fruits.popleft()
>>> 'apple'
>>> print(fruits)
deque(['banana', 'pear'])

Keeping the Last Few Items

>>> from collections import deque
>>> fruits = deque(['apple', 'banana', 'pear'], maxlen=3)
>>> fruits.append('mango')
>>> print(fruits)
deque(['banana', 'pear', 'mango'])

References

Previous
Next

Enum

Queue