Explain Stack in Python with examples


A stack is a linear data structure that works on the Last In First Out mechanism(LIFO). The element which enters first in the stack is the last to be processed.

Example

The stack data structure can be understood with the help of the example of a stack of dishes.

The dishes are piled up one upon the another. The first plate or dish is at the bottom of the pile and the last dish placed is on the top of the pile or stack. Whenever we need a plate, we will pick up the plate at the top of the stack which is the plate inserted or placed at the last. The plate which was placed first will be the last one to be picked up. Thus, the LAST IN FIRST OUT mechanism is followed.

Implementation of Stack in Python

The Stack in Python can be implemented in various ways using other linear data structures or in-built modules in Python library.

Method 1 − Implement using list

The stack in Python can be implemented using list. But, using list to implement stack is not much efficient and hence not recommended.

Operations involved

append() − This function adds an element at the end of the stack.

pop() − This function removes and returns the last or the top element in the stack.This function pops the elements in LIFO order.

Example

 Live Demo

stack=[]
stack.append(1)
stack.append(2)
stack.append(3)
print("Intial Stack",stack)
print("Element popped from the stack")
print(stack.pop())
print(stack.pop())
print("Stack after popping some elements",stack)

Output

Intial Stack [1, 2, 3]
Element popped from the stack
3
2
Stack after popping some elements [1]

You can’t remove more elements once the stack is empty. Doing so results in exception.

stack.pop()
IndexError: pop from empty list

Method 2 − Implement using queue.LifoQueue

This is the way to implement stack using inbuilt module from python. We need to import LifoQueue from queue. We can initialize LifoQueue or stack with some specific size . A size of zero means an infinite stack.

Operations involved

maxsize − maximum number of elements allowed in a stack

get() − remove and return the last or top element from the stack.If stack is empty, wait until stack has atleast one element.

get_nowait() − remove and return the last element from the stack.If stack is empty, raise an exception.

put(item) − append an element at the end of the stack.If stack is full, wait until a free slot is availabe.

put_nowait(item) − append an element at the end of the stack.If stack is full, raise an exception.

full() − returns true if the stack is full, else return false.

empty() − return True if the stack is empty, else false

qsize() − returns the number of elements present in the stack

Example

 Live Demo

from queue import LifoQueue
s=LifoQueue(maxsize=3)
s.put(1)
s.put(2)
s.put(3)
print("Is stack full",s.full())
print("Element popped from the stack")
print(s.get())
print(s.get())
print("Number of elements in stack",s.qsize())
print("Is stack empty",s.empty())

Output

Is stack full True
Element popped from the stack
3
2
Number of elements in stack 1
Is stack empty False

Method 3: Implement using collections.deque

This is another way to implement stack in Python. We need to import deque from collections module.

Operations involved

append() − This function adds an element at the end of the stack.

pop() − This function removes and returns the last element in the stack in O(1) time complexity.

Example

 Live Demo

from collections import deque
stack=deque()
stack.append(1)
stack.append(2)
stack.append(3)
print("Intial stack: ",stack)
print("Element popped from the stack")
print(stack.pop())
print(stack.pop())
print("Stack after popping some elements: ",stack)

Output

Intial stack: deque([1, 2, 3])
Element popped from the stack
3
2
Stack after popping some elements: deque([1])

Using pop() function on an empty deque will raise an exception.

Updated on: 11-Mar-2021

916 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements