Open In App

How to Use Lists as Stacks and Queues in Python

Last Updated : 21 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, lists can be used to implement a variety of data structures, including stacks and queues. In this article, we’ll explore how to use Python lists to create and perform basic operations on stacks and queues.

Using List as Stack in Python

A stack is a fundamental data structure that follows the Last In, First Out (LIFO) principle. In simpler terms, the last element added to the stack is the first one to be removed. Think of it like a stack of plates, we can only take the top plate off the stack, and to add a new plate, we place it on top.

The basic operations associated with a stack include:

Operation

Description

Push

Adding an element to the top of the stack.

Pop

Removing the element from the top of the stack.

Peek (or Top)

Viewing the element at the top of the stack without removing it.

IsEmpty

Checking if the stack is empty.

Below are some of the examples by which we can understand using list as a stack in Python:

Push Operation

In this example, a stack is initialized as an empty list, and elements are successively added to the top of the stack using the append method. The append method ensures the last item is added at the end of the list, simulating the push operation of a stack.

Python
li = []
li.append(1)
li.append(100)
li.append(23493)
print(li)

Output
[1, 100, 23493]

Pop Operation

The pop method removes and returns the last element from the stack, thus following the LIFO principle. After performing the pop operation, the stack shrinks, and the remaining elements are printed.

Python
li = [1, 2, 4, 4, 100, 212]

# Pop the top element from the stack
a = li.pop()

# Display the popped element and updated stack
print(a)
print(li)

Output
212
[1, 2, 4, 4, 100]

Top Operation

To view the top element without removing it, we can use negative indexing (-1) in Python. This provides a quick way to access the last element of the list.

Python
li = [1, 2, 4, 54, 3, 3532, 23]

# Access the top element without popping
a =li[-1]
print(a)

Output
23

IsEmpty Operation

To check whether the stack is empty, we can simply evaluate if the list has any elements. An empty list evaluates to False, while a non-empty list evaluates to True.

Python
# Initialize an empty stack
li = []

# Check if the stack is empty
if not li :
    print("empty")
else:
    print("not empty")

Output
empty.

Using List as Queues

A queue is another essential data structure that follows the First In, First Out (FIFO) principle. In a queue, the first element added is the first one to be removed. Queues are commonly used in scenarios where tasks or processes are executed in the order they are received. Some common operations associated with queues include:

Operation

Description

Enqueue

Adding an element to the rear (end) of the queue.

Dequeue

Removing the element from the front (head) of the queue.

Front

Viewing the element at the front without removing it.

Rear

Viewing the element at the rear without removing it.

IsEmpty

Checking if the queue is empty.

Below are some of the examples by which we can understand about using list as queues in Python:

Enqueue Operation

In Python, we can define a simple Queue class to simulate the enqueue operation. The enqueue method appends elements to the end of the list, simulating the addition of items to the queue.

Python
# Initialize an empty queue
a = []

# Enqueue Operation (adding elements to the queue)
a.append(10)
a.append(20)
a.append(30)
print(a) 

Output
[10, 20, 30]

Dequeue Operation

dequeue method removes and returns the first element from the queue. This operation simulates the behavior of a queue, where elements are processed in the order they arrive.

Python
# Initialize an empty queue
a = []


# removing the first element
if a:  # Check if the queue is not empty
    res = a.pop(0)
    print(res)
else:
    print("Empty")

Output
Empty

Front and Rear Operation

To view the front or rear elements of the queue without removing them, we can simply access the first or last element of the list using indexing.

Python
# Initialize queue
a = [10, 20, 30]

# Front Operation 
if a:  # Check if the queue is not empty
    res = a[0]
    print(res)
else:
    print("Empty")

# Rear Operation
if a:  # Check if the queue is not empty
    res = a[-1]
    print(res)
else:
    print("Empty")

Output
10
30

IsEmpty Operation

In a queue, this operation helps determine if there are any elements available to process or if the queue is completely empty.

Python
# checking if the queue is empty
a=[10, 20, 30]
res = len(a) == 0
print(res)

Output
False

Next Article

Similar Reads