Python Stack and Queue
Data structures are an important part of computer science because they provide organized ways to store, access, and modify data. Two the most fundamental data structures are Stack or Queue. They are widely used as the foundation for different algorithms and programming techniques.
Python’s flexible list can be used to implement a stack, while queues can be implemented using suitable Python queue structures. Stack follows the LIFO (Last In First Out) principle, whereas a queue follow FIFO (First In First Out) principle.
Table of Contents

Stack in Python
A stack is a linear data structure in which the last element added is the first element removed. A simple real-world example is a stack of plates, where the top plate is normally the first one taken away.
Basic Operations of a Stack
- Push: Adds an element to top of the stack.
- Pop: Remove an element from top of stack.
Stack Operations Explained
- Adding (Push): A new element is placed at the top of the stack, increasing its size.
- Deleting (Pop): If the stack contains elements, the top element is removed. Trying to remove an element from an empty stack results in an underflow condition.
- Traversing: Allows the elements of a stack to be visited and processed.
Key Characteristics of Stacks
- The insertion order is preserved according to the LIFO principle.
- Duplicate values are allowed.
- Stacks are commonly used in parsing operations and algorithmic problem-solving.
Python Code for Stack Implementation
Python list can be use to implement basic stack operations. The append() method performs the push operation, while pop() removes the topmost element.
# Implementation of stack using a list
x = ["Python", "C", "Android"]
x.append("Java") # Push operation
x.append("C++") # Push operation
print(x) # Display the stack
print(x.pop()) # Pop the topmost element
print(x)
print(x.pop())
print(x)
Output:
['Python', 'C', 'Android', 'Java', 'C++']
C++
['Python', 'C', 'Android', 'Java']
Java
['Python', 'C', 'Android']
Queue in Python
A queue is linear data structure that follow FIFO (First In First Out) principle. This means that the element added first is the element removed first.
A simple real-world example is a line of people waiting for service. The person who arrives first is generally served before the people who arrive later.
Basic Operations of a Queue
- Enqueue: Adds an element to the end of the queue.
- Dequeue: Removes an element from the front of the queue.
Queue Operations Explained
- Adding (Enqueue): Elements are added at the back or end of the queue.
- Deleting (Dequeue): The element at the front is removed. If the queue is empty, an underflow condition occurs.
- Traversing: Allows the elements in the queue to be visited and processed.
Key Characteristics of Queues
- The order of insertion is preserved according to the FIFO principle.
- Duplicate values are allowed.
- Queues are commonly use to scheduling task such as CPU processing and resource management.
Python Code for Queue Implementation
Python provide a queue module for creating queue structures. The put() method adds elements, while get() removes them from the queue.
import queue
# Creating a queue with a maximum size of 10
L = queue.Queue(maxsize=10)
# Adding elements to the queue
L.put(9)
L.put(6)
L.put(7)
L.put(4)
# Removing elements from the queue
print(L.get())
print(L.get())
print(L.get())
print(L.get())
Output:
9
6
7
4
Stack vs. Queue
| Feature | Stack | Queue |
|---|---|---|
| Principle | LIFO (Last In First Out) | FIFO (First In First Out) |
| Addition | Push | Enqueue |
| Removal | Pop | Dequeue |
| Typical Applications | Backtracking and recursive parsing | Task scheduling and sequential processing |
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
YT:- DecodeIT
Efficiency of Stack and Queue
Stack operations such as append() and pop() at the end of a Python list are generally efficient because they operate at the end of the list.
Queue operations can be less efficient when a regular list is used for removing elements from the beginning because remaining elements may need to be shifted. The provided queue.Queue structure is designed specifically for queue operations.
Frequently Asked Questions
1. What is a stack in Python?
A stack is a data structure that follows the LIFO principle, meaning the last element inserted is the first element removed.
2. What is a queue in Python?
A queue is a data structure that follows the FIFO principle, meaning the first element inserted is the first element removed.
3. What is the difference between stack and queue?
A stack uses LIFO, while a queue uses FIFO. In a stack, the most recently added element is removed first; in a queue, the earliest added element is removed first.
4. How do you implement a stack in Python?
A Python list can be used as a stack by using append() to push elements and pop() to remove the top element.
5. Which Python module is used for a queue?
Python provides the built-in queue module, which includes the Queue class for implementing queues.
6. What are the main operations of a stack?
The two basic stack operations are push, which adds an element, and pop, which removes the top element.
7. What are the main operations of a queue?
The basic queue operations are enqueue, which adds an element to the rear, and dequeue, which removes an element from the front.
Conclusion
Python Stack and Queue are fundamental data structures that help organize and process data according to different access patterns. A stack follows LIFO, while a queue follows FIFO.
Python lists provide a simple way to implement stack operations, while the queue module provides a dedicated queue structure. Understanding these two data structures is important for learning algorithms, solving programming problems, and building more advanced applications.
Keywords: Python Stack and Queue, python stack and queue W3Schools, python stack and queue GeeksforGeeks, python queue, stack and queue in Python questions, using list as stack and queue in Python, stack in Python W3Schools, difference between stack and queue, Python Stack and Queue tutorial, Python stack implementation, Python queue implementation