Exploring Deques in Python
≀Paulo Portela
Posted on February 19, 2024
In this post, we'll explore deques in Python, using an astronomy theme to provide code examples. Deques, short for double-ended queues, are a generalization of stacks and queues. They support adding and removing elements from both ends with approximately the same O(1) performance. Let's get started.
First, let's create a deque of planets in our solar system:
from collections import deque
planets = deque(["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"])
print(planets)
# Output: deque(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'])
In this example, we create a deque called planets
containing the names of the eight planets in our solar system.
We can add elements to either end of a deque using the append
and appendleft
methods:
planets.append("Pluto")
print(planets)
# Output: deque(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
planets.appendleft("Ceres")
print(planets)
# Output: deque(['Ceres', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune', 'Pluto'])
In this example, we add the dwarf planet Pluto to the right end of our deque of planets using the append
method, and the dwarf planet Ceres to the left end using the appendleft
method.
We can remove elements from either end of a deque using the pop
and popleft
methods:
pluto = planets.pop()
print(pluto)
# Output: Pluto
ceres = planets.popleft()
print(ceres)
# Output: Ceres
In this example, we remove and return the rightmost element from our deque of planets using the pop
method, and the leftmost element using the popleft
method.
We can also insert elements into a deque at a specific position using the insert
method:
planets.insert(3, "The Moon")
print(planets)
# Output: deque(['Mercury', 'Venus', 'Earth', 'The Moon', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'])
In this example, we insert the Moon into our deque of planets at position 3 using the insert
method.
We can remove a specific element from a deque using the remove method:
planets.remove("The Moon")
print(planets)
# Output: deque(['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'])
In this example, we remove the Moon from our deque of planets using the remove
method.
We can also rotate a deque by a specified number of steps to the right using the rotate
method:
planets.rotate(3)
print(planets)
# Output: deque(['Saturn', 'Uranus', 'Neptune', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter'])
In this example, we rotate our deque of planets three steps to the right using the rotate
method.
That concludes our introduction to deques in Python. We've covered the basics of creating and accessing deques, as well as some of their useful methods. Deques are a powerful tool for working with collections of elements, and can be a useful addition to your Python toolkit.
Posted on February 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 12, 2024