coding180 icon
Coding180
Stacks: Introduction, push and pop operations, stack implementations: examples and exercises

Stacks: Introduction, push and pop operations, stack implementations: examples and exercises

Home > Tutorials > Python for beginners > Data structure


A stack is an abstract data type that stores a collection of elements, which can be pushed or popped in a last-in-first-out (LIFO) order. In other words, the most recently added item is the first one to go out.

There are two primary operations associated with stacks, push and pop. When you push an element into a stack, it gets added to the top of the stack. When you pop an element from a stack, you remove the top-most element from it.

Push and Pop Operations

Let's take a closer look at both push and pop operations:

Push Operation

This operation adds an element to the top of the stack. In Python, you can use the append() function to add an element to a list. Since stacks can be implemented using lists in Python, we can use this function to push an element onto the stack.

Here's an example:

stack = [1, 2, 3]
stack.append(4)
print(stack)

Output:

[1, 2, 3, 4] 

 

Pop Operation

This operation removes the top-most element from the stack. In Python, you can use the pop() function to remove an element from a list. Since stacks can be implemented using lists in Python, we can use this function to pop an element from the stack.

Here's an example:

stack = [1, 2, 3, 4]
stack.pop()
print(stack)

Output:

[1, 2, 3]

 

Stack Implementations

There are several ways to implement stacks in Python. Let's discuss a few of them:

Using Lists

One of the most straightforward ways to implement stacks in Python is by using lists. You can add elements to the top of the stack using the append() function and remove elements from the top using the pop() function.

Here's an example of a simple stack implementation using lists:

stack = []

stack.append(1)
stack.append(2)
stack.append(3)

print("Initial Stack:", stack)

stack.pop()

print("After one pop:", stack)

Output:

Initial Stack: [1, 2, 3]
After one pop: [1, 2]

Using Deque

Another way to implement a stack in Python is by using the deque class from the collections module. The deque class provides a double-ended queue, which can be used as a stack or queue.

Here's an example of how you can use deque to implement a stack:

from collections import deque

stack = deque()

stack.append(1)
stack.append(2)
stack.append(3)

print("Initial Stack:", stack)

stack.pop()

print("After one pop:", stack)

Output:

Initial Stack: deque([1, 2, 3])
After one pop: deque([1, 2])

Using LifoQueue

Python also provides LifoQueue class from the queue module that can be used to implement a stack. The LifoQueue class implements a queue data structure, but with stack-like behavior.

Here's an example of how you can use LifoQueue to implement a stack:

from queue import LifoQueue

stack = LifoQueue()

stack.put(1)
stack.put(2)
stack.put(3)

print("Initial Stack:", list(stack.queue))

stack.get()

print("After one pop:", list(stack.queue))

Output:

Initial Stack: [1, 2, 3]
After one pop: [1, 2]

Exercises

Here are some exercises to practice your Python stack implementation skills:

Exercise 1

Implement a stack using a list and write a function is_empty() that returns True if the stack is empty, and False otherwise.

Exercise 2

Implement a stack using a list and write a function get_size() that returns the number of elements in the stack.

Exercise 3

Implement a stack using deque and write a function clear_stack() that empties the stack.

Exercise 4

Implement a stack using LifoQueue and write a function get_top_element() that returns the top-most element of the stack without removing it.

Conclusion

In this article, we covered the basics of Python stacks, push and pop operations, and implemented stacks using lists, deque, and LifoQueue. We also provided some exercises to help you practice your stack implementation skills.

Studying stacks is an essential part of learning computer science and programming. Stacks can be used in various applications like backtracking, expression evaluation, call stack management, and much more.

So mastering this concept will be beneficial for coding in Python as well as other programming languages.


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel