Stack in Python
In this tutorial, we will learn the basics of the stack and implement it using the Python code.
What is a Stack?
A stack is a linear data structure where data is arranged objects on over another. It stores the data in LIFO (Last in First Out) manner. The data is stored in a similar order as plates are arranged one above another in the kitchen. The simple example of a stack is the Undo feature in the editor. The Undo feature works on the last event that we have done.
We always pick the last plate from the stack of the plate. In stack, the new element is inserted at the one end and an element can be removed only that end.
We can perform the two operations in the stack - PUSH and POP. The PUSH operation is when we add an element and the POP operation is when we remove an element from the stack.
Methods of Stack
Python provides the following methods that are commonly used with the stack.
Implementation of Stack
Python offers various ways to implement the stack. In this section, we will discuss the implementation of the stack using Python and its module.
We can implement a stack in Python in the following ways.
Implementation Using List
Python list can be used as the stack. It uses the append() method to insert elements to the list where stack uses the push() method. The list also provides the pop() method to remove the last element, but there are shortcomings in the list. The list becomes slow as it grows.
The list stores the new element in the next to other. If the list grows and out of a block of memory then Python allocates some memory. That's why the list become slow. Let's understand the following example -
['x', 'y', 'z'] Elements poped from my_stack: z y x my_stack after elements are poped:  Traceback (most recent call last): File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/Stack.py", line 21, in <module> print(my_stack.pop()) IndexError: pop from empty list
In the above code, we have defined an empty list. We inserted the elements one by one using the append() method. That is similar to the push() method. We also removed the elements using the pop() method. The pop() method returns the last element of the list.
Implementation using collection.deque
The collection module provides the deque class, which is used to creating Python stacks. The deque is pronounced as the "deck" which means "double-ended queue". The deque can be preferred over the list because it performs append and pop operation faster than the list. The time complexity is O(1), where the list takes O(n).
Let's understand the following example -
Initial my_stack: deque(['a', 'b', 'c']) Elements poped from my_stack: c b a my_stack after elements are poped: deque()
The above code is almost similar to the previous example. However, the only difference is that, we have imported the deque from the collection module.
Deque Vs. list
The list stores the element right next to each other and uses the contiguous memory. This is most effective for the several operations, like indexing into the list. For example - Getting list1 is fast as Python knows the exact position of a particular element. The contiguous memory also allows slice to work well on the lists.
The list consumes extra time to append() some objects than others. If the block of contiguous memory is full, it will get another block that can take much a longer time than a normal append() function.
Implementation Using queue module
The queue module has the LIFO queue, which is the same as the stack. Generally, the queue uses the put() method to add the data and the () method to take the data.
Below are a few methods that available in the queue.
Let's understand the following example of the queue module.
0 Stack is Full: False Size of Stack: 3 Elements poped from the my_stack z y x Stack is Empty: True
In the above, we have imported the queue module that is a LIFOqueue. It works the same as the stack but this module includes some additional functions mentioned above. We defined a stack with the maxsize that means it can hold maximum five values in it.
The initial array size is zero; we pushed three elements in the stack using the put() method. Now, again we checked whether a stack is empty and size of the stack. We have three elements in the stack. We popped the element using the get() method. It removes the last added element first. After removing the entire elements, we get an empty stack.
Python Stacks and Threading
We can also use Python stack in the multi-threaded program. It is an advanced topic but not frequently used so you can skip this section if you are not interested in threading.
The list and deque behave differently if we are using threading in our program. Using a list in multi-threading programming can be dangerous because it is not thread-safe.
On the other hand, the deque is a little bit complex because its append() and pop() methods are atomic, which means they will not interrupt by other thread.
We can build the multithreading program using the deque, but it may cause few complexities in the future.
Now the question arises, how we can build a program of Python stack with threading.
The answer is that we can use the LIFOqueue and we know that what LIFO stands for - Last In First Out. It uses the put() and gets() to add and remove the stack element.
Which Implementation of Stack Should Consider?
We have mentioned three methods of implement the stack in Python. The above methods have their own advantages or disadvantages.
Let us cleat the confusion; we are using stack with the threading, you should use the Lifoqueue but make sure about its performance for popping and pushing elements. But if you are not using threading, use a deque.
We can also use the list to implement the stack but the list can have potential memory reallocation issues. The list and deque are same in the interface, but the deque doesn't memory allocation issues.
We have defined a stack and its implementation in brief. Python stack can be used in real-life programs. We can choose either implement the method according to our requirements. We have also defined the Python stack with threading environment.