Python Lists vs. Deques: Practical Considerations and Trade-offs

Zalwert
5 min readApr 4, 2024

Why you should consider using deques instead of lists in your projects (really?)

generated by AI (StableDiffusionXL on poe.com)

I bet you’re mostly using lists in your python projects, which is OK, but not always optimal.

In most cases, you can use deques as an alternative to lists. This can significantly speed up your program’s performance and gives you more interesting built-in functionalities.

deque in Python, short for "double-ended queue", is indeed similar to a list in some ways, but it offers specific features that make it useful for certain scenarios.

Let’s first understand what a list and a deque are in Python.

FYI: here’s an official documentation for lists and deques

Lists in Python

A list in Python is a versatile data structure used to store a collection of elements. It’s ordered and mutable, allowing for flexible manipulation.

# List example
my_list = [1, 2, 3, 4, 5]

# Append an element to the end of the list
my_list.append(6)
print("List after appending an element:", my_list)

# Extend the list with another iterable
my_list.extend([7, 8, 9])
print("List after extending:", my_list)

# Pop an element from the end of the list
popped_element = my_list.pop()
print("Popped element from…

--

--

Zalwert

Experienced in building data-intensive solutions for diverse industries