The above figure is a good example of stack data structure in Python. Suppose stones arranged on top of each other are elements of stack now the last stone added is the topmost one and if we want to remove a stone we will remove the last one added that is from the top. So we can observe a pattern of Last In First Out(LIFO) i.e. the last element added is the first element retrieved. That‘s so simple.
We already read a data structure that can be used as a stack and guess which one? The answer is a List. Yes, because in the list using append we can add elements to the end, and using pop we can remove the element that was added last.
Let’s create a list:
stack = [1,2,3,4,5]
Here we have a list named stack with some values init.
Using the append method we can add elements to the end: