Search⌘ K
AI Features

Stacks and Queues

Explore how Python 3 implements stacks and queues using built-in data structures like lists and collections.deque. Understand the performance differences and appropriate use cases for each to write efficient and clean code. This lesson helps you choose the right data structure based on problem needs.

This lesson lists different implementations of stack and queue provided by Python 3 and explains when to use which built-in support.

Introduction

A stack is a data structure that follows the LIFO (Last In First Out) order for push (insertion) and pop (deletion) functions.

A queue is a data structure that follows the FIFO (First In First Out) order for enqueue (insertion) and dequeue (deletion) functions.

Stack
Stack
Queue
Queue

Stack and queue implementation

Using list

As you know, list is one of the basic built-in data structures in Python. It allows random access in O(1)O(1) time using the indexing ...