Sobes.tech
Junior — Middle

How do you create a stack data structure in Python?

sobes.tech AI

Answer from AI

In Python, a stack can be implemented using a list, utilizing the append() method to add an element and the pop() method to remove the last added element, which corresponds to the LIFO (Last In, First Out) principle.

Example of stack implementation:

stack = []

# Adding an element
stack.append(1)
stack.append(2)

# Removing an element
top = stack.pop()  # top will be 2