Python Stack: The Complete Guide

In this article, we will be looking at a fundamental data structure called a Stack in Python. Firstly, let us understand Stack with a use case where we are using a stack as a data structure.

Consider a recipe website, and on that website, we can navigate through various pages. For example, I am reading Recipe A, and then I want to read Recipe B, then I want to read Recipe C.

When I go through all these pages, the browser maintains a history of them, and when I press the back button, it will show me the last page that I have visited. So, I saw Recipe B, and before that, I visited the Recipe A page, and before that, I was on a home page.

Here, we can imagine, if we are a browser developer then, how we would manage this history in terms of data structure?

One option is that we can use an array to store all these links, and whenever we want to get the visited link, we have to go to the end of the array and pop that element out.

Another option is linked lists! But the issue with this data structure is that, for example, if we are using a linked list, we have to traverse the whole linked list to go to the end, and that end will give us the last link that we visited.

The same kind of problem goes with array as well! We have to use a dynamic array because we can’t use a static array. After all, it can just keep on growing.

With dynamic arrays also, there are issues such as memory relocation and many more. 

So, in this situation, imagine we have a data structure so that once we visit a link, we can keep pushing the elements to that particular data structure. And when we click on the back button, we can retrieve the last element that we have pushed. And this type of data structure is called STACK.

Python Stack

A stack in Python is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. It is a linear data structure where we keep on pushing the elements from the rear end, and when we say pop, it will pop out the last element that we have pushed.

In Python, we can implement Stack using list, deque from collections module, and LifoQueue from queue module.

This article will focus only on implementing a stack using the list.

To implement Stack using list, the following are the operations performed on the Stack using list,

Stack push() function

The push() is a built-in function that pushes the element into the Stack.

Syntax

 append(element)

Stack pop() function

The pop() is a built-in Python function that pops/removes an element in the Stack from the end.

Syntax

Stack_name.pop()

Stack top() function

The top() is a built-in stack function that gives the latest element, which we have pushed into the Stack.

Syntax

Stack_name[-1]

Stack size() function

The size() is a built-in function that gives the size of the Stack.

Syntax

len(Stack_name)

Note

  1. Push operation means adding elements to the Stack.
  2. Pop operation means to delete an element from the Stack.

So now, let us get started with the code,

STEP 1: Declare and initialize the Stack.

pyStack = [ ]

Here the time complexity is O(1).

STEP 2: Append the items to the Stack and print the content of the Stack.

pyStack.append("Design")
print(pyStack)

Output

['Design']

See the following complete code.

pyStack = []

pyStack.append("Design")
pyStack.append("Public Relation")
pyStack.append("Finance")
pyStack.append("Sponsorship")

print(pyStack)

Output

['Design', 'Public Relation', 'Finance', 'Sponsorship']

We implemented the push operation using the append() function in the above code.

Here, we can see that the elements are added at last.

Here, the time complexity of the operation, i.e., the “append()” function, is O(1).

STEP 3: Let’s find the top element of the Stack.

pyStack = []

pyStack.append("Design")
pyStack.append("Public Relation")
pyStack.append("Finance")
pyStack.append("Sponsorship")

print(pyStack[-1])

Output

Sponsorship

The time complexity to find the topmost element is O(1).

STEP 4: Let us pop some elements from the Stack.

pyStack = []

pyStack.append("Design")
pyStack.append("Public Relation")
pyStack.append("Finance")
pyStack.append("Sponsorship")

print(pyStack)
print(pyStack.pop())
print(pyStack)
print(pyStack.pop())
print(pyStack)
print(pyStack.pop())
print(pyStack)

Output

['Design', 'Public Relation', 'Finance', 'Sponsorship']
Sponsorship
['Design', 'Public Relation', 'Finance']
Finance
['Design', 'Public Relation']
Public Relation
['Design']

The stack pop() function removes the element from the Stack from the rear end and thus implements Last In First Out or First In Last Out.

The time complexity of the stack pop() function is O(1).

Summary

Stack is a linear data structure that uses the Last In First Out(LIFO) approach. We can push(add) the element and pop(delete) the element from the rear end. We can implement Stack in Python using list, deque from collections module, and LifoQueue from queue module.

In this article, we implemented a stack using a list.

We can perform various operations on the Stack, such as

  1. Push operation, which means to add the element to the Stack.
  2. The pop operation means to delete the element from the Stack (from the rear end).
  3. The top operation gives us the reference of the topmost element.
  4. The empty operation tells us whether the Stack is empty or not.

That’s it for Stack in Python.

Related posts

How to Sort 2D Array in Python

How to Tabulate JSON in Python

How to Sort a List of Lists in Python

Leave a Comment