A
A
Alexey Bentley2021-12-09 13:01:28
Python
Alexey Bentley, 2021-12-09 13:01:28

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

2 answer(s)
R
Roman Kitaev, 2021-12-09
@deliro

list is already the most classic stack. adding to the end in O(1). deleting from the end is also in O(1)

V
Vladimir Kuts, 2021-12-09
@fox_12

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 question

Ask a Question

731 491 924 answers to any question