A brief overview of Stack in Python

Nilson Chapagain
Analytics Vidhya
Published in
2 min readJul 20, 2021
Photo by Bekir Dönmez on Unsplash

Stack

A stack is a collection of objects that are inserted and removed according to the Last‐In, First‐Out (LIFO) principle.

Refer to the image above and the exact principle to remove objects. We remove from the top and insert on the top, hence, last in first out.

Main operations on stacks

  • Push: adding an element to a stack.
  • Pop: removing the most recently added entry from a stack.
  • Peek: access to the top element without modifying the stack.

Stack using List with a Wrapper Class

class Stack():

def __init__(self):
self.stack = list()

def push(self, item):
self.stack.append(item)

def pop(self):
if len(self.stack) > 0:
return self.stack.pop()
else:
return None

def peek(self):
if len(self.stack) > 0:
return self.stack[len(self.stack)-1]
else:
return None

def __str__(self):
return str(self.stack)

Run code:

# creating stack object
my_stack = Stack()


my_stack.push(1)
my_stack.push(2)
my_stack.push(3)
print('Complete stack as of now')
print(my_stack)

print('\nStack after applying peek')
print(my_stack.peek())
print(my_stack)

print('\nStack after applying pop once')
print(my_stack.pop())
print(my_stack)


print('\nStack after applying peek')
print(my_stack.peek())
print(my_stack)
# Results: *******************************************************Complete stack as of now
[1, 2, 3]

Stack after applying peek
3
[1, 2, 3]

Stack after applying pop once
3
[1, 2]

Stack after applying peek
2
[1, 2]

Interesting details

  • The push and pop operations occur only at one end of the structure, referred to as the top of the stack.
  • This data structure makes it possible to implement a stack as a singly linked list and a pointer to the top element.
  • A stack may be implemented to have a bounded capacity.
  • If the stack is full and does not contain enough space to accept an entity to be pushed, the stack is then considered to be in an overflow state.

Todo for you:

Add the implementations to provide a check if the stack is empty and one that returns its size.

Use cases

  • A stack is used to implement depth-first search (Backtracking*).
  • Expressions can be represented in the prefix, postfix, or infix notations, and conversion from one form to another may be accomplished using a stack.
  • Many compilers use a stack for parsing the syntax of expressions, program blocks, etc. before translating into low-level code.
  • The nearest-neighbor chain algorithm, a method for agglomerative hierarchical clustering based on maintaining a stack of clusters, each of which is the nearest neighbor of its predecessor on the stack.

Now that you are familiar with the concept of stacks visit Leet code to practice some real-life problems and solve them using the idea summed up in this brief overview. All the best!

Special thanks to Wikipedia* and python documentation* for the valuable information, which I used in this article, to sum up, the crucial concept.

Thank you for reading my post. I hope that you have learned something useful* 🙌 🎉

--

--