Chances are you’ve used lists, dictionaries, tuples, and maybe a set in your code. These are your basic built-in data structures Python has to offer you.
There’s a few data structures you should be aware of that are not built in, especially if you’re looking to work at a FAANG/MANGO company.
These data structures are stacks and queues - they’re built into the Python standard library, but they can also be built yourself using lists.
Stack
Think of a stack like a glass full of M&M’s. Once a bunch of M&Ms goes into the glass, you can’t get to the bottom ones unless you either:
Remove the ones from the top
Reach your fingers all the way down to the bottom (please don’t do this though)
A stack is a LIFO structure: last in, first out. The way we insert into the queue is last item in, and the last item that was inserted is the first one out.
If you’ve ever used the “undo” button, it uses a stack to save the most recent action you did.
Each time you do a task, it saves what you did in the stack, with the very first task you did at the bottom all the way to the most recent task you did at the top.

Image of stack
Stacks can be created using the queue.LifoQueue class:
from queue import LifoQueue
q = LifoQueue()
q.put("Hello")
q.put("World!") # <-- Item that's on the top of the stack
q.queue # ["Hello", "World!"]
q.get() # ["Hello"]
q.get() # []Queue
Think of a queue as a line at an amusement park; unless you’re one of the lucky ones without an overpriced FastPass, you have to stand in line and wait your turn.
The first person who gets in line is the first person who gets onto the ride. The second person to get in line is the second person to get on the ride, and so forth.
Queues are a data structure that allows you to “enqueue” (add to the queue) in the back and then “dequeue” (remove from the queue) the front:

Depiction of a queue
Queues are a first-in, first-out data structure (FIFO), primarily designed for producer-consumer systems. A really good example of when you’d use a queue is if you have multiple processes collecting data and trying to write to a file at the same time.
It would make sense to have those individual processes to write to a file, right? Not necessarily - if you do, you could run into something like a race condition.
Instead, you’ll put your data into a queue and let some different process take care of handling your data and to the writing to the file (say, a csv for this example) to avoid issues you really shouldn’t be running into.
To use a queue, import the queue module:
from queue import Queue
q = Queue()
# Add to the queue ("enqueue")
q.put("hello")
q.put("goodbye")
q.put(1)
# dequeue each value and show it
for _ in range(q.qsize()):
print(q.get())Which will print the following:
"hello"
"goodbye"
1Double Ended Queues
Just like a regular queue, a double ended queue (“deque”) allows you to add and remove items on both sides:

Double ended queue
Although really uncommon, there are some situations where you may need to use this:
A sliding window problem: if you’re working with data that requires you to move left and right with your timeframe (i.e. looking at data between 10PM and 12PM, but then updating it to include data between 11PM and 1AM).
Maintaining browser history with a limit. You could use a stack, but this forces you to have an unbounded browser history, which when scaled will eat up memory over time.
Thankfully, Python includes a double ended queue in it’s collections library to make things simple (and thread-safe!):
from collections import deque
d = deque()
d.append(1) # deque([1])
d.appendleft(2) # deque([2, 1])
d.popleft() # deque([1])Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

