Answer the question
In order to leave comments, you need to log in
Stack implementation in python?
how to implement stack in python?
with adding elements via push and removing elements via pop
Answer the question
In order to leave comments, you need to log in
list is already the most classic stack. adding to the end in O(1). deleting from the end is also in O(1)
Alternatively, you can use Queue , or collections.deque
from collections import deque
q = deque()
q.append('10') # push
q.append('20') # push
q.append('30') # push
print(q)
# deque(['10', '20', '30'])
print(q.pop()) # pop
# 30
print(q.pop()) # pop
# 20
print(q)
# deque(['10'])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question