In our previous tutorial we learned about Python signal processing. In this tutorial we will learn about python stack.
To start with this tutorial, you have to know what is stack at first. Basically stack is Last-In-First-Out data structure. That means, the item that entered last in the system will be removed at first.
For example, let’s say you have a pipe with only one open end. And you have some balls that fit just fine in the pipe. So when you will insert these balls in the pipe, they will get stacked on top of each other. However when you will remove the balls, the last one will be removed first.
Python Stack push
Well, there is no additional data structure as python stack. But as stack is a very common data structure, we will try to implement that.
So if you have already studied about stack, you should know that stack has two operations. One is
push and another is
pop. In this section we will be discussing about push operation. Push operation is used to add items to the stack. As we will be using Python List as stack, we can use
append() function to push items into the list.
stack =  # initialize a list as stack print('current stack :', stack) # add items to the stack for i in range(5): # push items into stack stack.append(i) print('current stack :', stack,'\tstack size :', len(stack))
The output of above stack implementation example will be like below image.
Python Stack pop
Now we need to learn how to pop items from stack. Python List has a function named
pop() that removes the last element from the list. But before removing you have to check if the stack is empty. So, if we add pop implementation to the previous code, then the final code will be like below.
stack =  # initialize a list as stack print('\ncurrent stack :', stack) print('\nPush items to the stack') # add items to the stack for i in range(5): # push items into stack stack.append(i) print('current stack :', stack,'\tstack size :', len(stack)) print('\nPop items from te stack') # now pop items from the stack while len(stack) > 0: # check if the stack is empty stack.pop() print('current stack :', stack, '\tstack size :', len(stack))
So, the output will be as like below.
Push items to the stack current stack :  stack size : 1 current stack : [0, 1] stack size : 2 current stack : [0, 1, 2] stack size : 3 current stack : [0, 1, 2, 3] stack size : 4 current stack : [0, 1, 2, 3, 4] stack size : 5 Pop items from te stack current stack : [0, 1, 2, 3] stack size : 4 current stack : [0, 1, 2] stack size : 3 current stack : [0, 1] stack size : 2 current stack :  stack size : 1 current stack :  stack size : 0
Python Stack implementation
As you see that we can utilize List append() and pop() functions to create our Stack implementation class. Here is a Stack implementation class with example.
class Stack: stack =  # empty list max_size = -1 def __init__(self, size=-1): # defining maximum size in the constructor self.max_size = size def push(self, item): if self.max_size == -1: # if there is no limit in stack self.stack.append(item) elif len(self.stack) < self.max_size: # if max limit not crossed self.stack.append(item) else: # if max limit crossed print('Can\'t add item. Stack limit is :', self.max_size) raise RuntimeError def pop(self): if len(self.stack) > 0: temp = self.stack[-1] self.stack.pop() return temp else: print('stack is already empty.') raise IndexError def top(self): if len(self.stack) > 0: return self.stack[-1] # returns the last item else: print('stack is already empty.') raise IndexError stack = Stack() stack.push(1) # push item 1 stack.push(2) # push item 2 print('Pop the last item :', stack.pop()) # pop the top item print('Current top item is :', stack.top()) # current top item
The above code will output like this
Pop the last item : 2 Current top item is : 1
So, that’s all about python stack. If you have any confusion regarding python stack implementation, please use the comment box.
Reference: Official Guide