A deque, or double-ended queue, is a special list that lets you add and remove items from both the front and the back. This makes it super useful for many tasks, like checking for palindromes (words that read the same forwards and backwards), storing data temporarily (caching), and even building other types of lists like queues and stacks.
Let's look at how to use deques in two programming languages: Python and Java.
Python has a built-in tool called the collections
module. Inside this module, there’s a deque
class that is great for managing a deque. It allows you to do various tasks easily.
Getting Started with deque
:
First, you need to import deque
:
from collections import deque
You can create a deque using a list of items or start with an empty one:
# Create a deque
my_deque = deque([1, 2, 3, 4]) # This deque has four numbers
empty_deque = deque() # This deque is empty
Common Actions You Can Do:
Add items to the right side using append()
:
my_deque.append(5) # Now it looks like: deque([1, 2, 3, 4, 5])
Add items to the left side using appendleft()
:
my_deque.appendleft(0) # Now it’s: deque([0, 1, 2, 3, 4, 5])
Remove items from the right side using pop()
:
last_item = my_deque.pop() # last_item = 5, now it looks like: deque([0, 1, 2, 3, 4])
Remove items from the left side using popleft()
:
first_item = my_deque.popleft() # first_item = 0, now it’s: deque([1, 2, 3, 4])
Using collections.deque
is really fast for these actions because they happen in constant time.
In Java, you can create a deque using two options: ArrayDeque
or LinkedList
, both from the java.util
package. Here’s how to set them up:
Using ArrayDeque:
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Adding items
deque.addLast(1); // Deque: [1]
deque.addLast(2); // Deque: [1, 2]
deque.addFirst(0); // Deque: [0, 1, 2]
// Removing items
int first = deque.removeFirst(); // first = 0, Deque: [1, 2]
int last = deque.removeLast(); // last = 2, Deque: [1]
}
}
Using LinkedList:
You can also do it with LinkedList
:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> deque = new LinkedList<>();
// Adding items
deque.addLast(1); // Deque: [1]
deque.addLast(2); // Deque: [1, 2]
deque.addFirst(0); // Deque: [0, 1, 2]
// Removing items
int first = deque.removeFirst(); // first = 0, Deque: [1, 2]
int last = deque.removeLast(); // last = 2, Deque: [1]
}
}
Both Python and Java make it easy to work with deques using their own tools. Which one you choose depends on what you need for your project and how you like to code.
Deques are very helpful in many programs and tasks, showing how important they are in learning about different ways to organize data.
A deque, or double-ended queue, is a special list that lets you add and remove items from both the front and the back. This makes it super useful for many tasks, like checking for palindromes (words that read the same forwards and backwards), storing data temporarily (caching), and even building other types of lists like queues and stacks.
Let's look at how to use deques in two programming languages: Python and Java.
Python has a built-in tool called the collections
module. Inside this module, there’s a deque
class that is great for managing a deque. It allows you to do various tasks easily.
Getting Started with deque
:
First, you need to import deque
:
from collections import deque
You can create a deque using a list of items or start with an empty one:
# Create a deque
my_deque = deque([1, 2, 3, 4]) # This deque has four numbers
empty_deque = deque() # This deque is empty
Common Actions You Can Do:
Add items to the right side using append()
:
my_deque.append(5) # Now it looks like: deque([1, 2, 3, 4, 5])
Add items to the left side using appendleft()
:
my_deque.appendleft(0) # Now it’s: deque([0, 1, 2, 3, 4, 5])
Remove items from the right side using pop()
:
last_item = my_deque.pop() # last_item = 5, now it looks like: deque([0, 1, 2, 3, 4])
Remove items from the left side using popleft()
:
first_item = my_deque.popleft() # first_item = 0, now it’s: deque([1, 2, 3, 4])
Using collections.deque
is really fast for these actions because they happen in constant time.
In Java, you can create a deque using two options: ArrayDeque
or LinkedList
, both from the java.util
package. Here’s how to set them up:
Using ArrayDeque:
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Adding items
deque.addLast(1); // Deque: [1]
deque.addLast(2); // Deque: [1, 2]
deque.addFirst(0); // Deque: [0, 1, 2]
// Removing items
int first = deque.removeFirst(); // first = 0, Deque: [1, 2]
int last = deque.removeLast(); // last = 2, Deque: [1]
}
}
Using LinkedList:
You can also do it with LinkedList
:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> deque = new LinkedList<>();
// Adding items
deque.addLast(1); // Deque: [1]
deque.addLast(2); // Deque: [1, 2]
deque.addFirst(0); // Deque: [0, 1, 2]
// Removing items
int first = deque.removeFirst(); // first = 0, Deque: [1, 2]
int last = deque.removeLast(); // last = 2, Deque: [1]
}
}
Both Python and Java make it easy to work with deques using their own tools. Which one you choose depends on what you need for your project and how you like to code.
Deques are very helpful in many programs and tasks, showing how important they are in learning about different ways to organize data.