The methods available with list make it very easy to use as a stack, where the last element added is the first retrieved. To retrieve an item from the top of stack, use append(). To retrieve an item from the top of the stack, use pop() without any explicit index. Lets understand it with an example.
stack_demo = [1, 5, 6, 7, 8, 9]
stack_demo.append(10)
print(stack_demo)
# Output
[1, 5, 6, 7, 8, 9, 10]
top_element = stack_demo.pop()
print(top_element)
print(stack_demo)
# Output
10
[1, 5, 6, 7, 8, 9]
Similar Articles