FIFO and LIFO queue examples in Python


Python tip:

You can create a simple FIFO or LIFO queue with the queue module.

An example👇

import queue

my_queue = queue.Queue()

my_queue.put("Jan")
my_queue.put("Mike")

print(my_queue.get())
# => Jan
print(my_queue.get())
# => Mike

my_lifo_queue = queue.LifoQueue()

my_lifo_queue.put("Jan")
my_lifo_queue.put("Mike")

print(my_lifo_queue.get())
# => Mike
print(my_lifo_queue.get())
# => Jan