Is there a standard "stack" data structure in Python?

Asked

Viewed 185 times

4

If not, do you know of a good non-standard module? I would need to work with stack, but I find it very strange that there is no module stack (stack) standard in Python and at the same time there is a module queue (queue).

1 answer

4


No. The standard language recommendation is use a list as a stack (stack).

In the case of a queue (Queue), additional methods and restrictions are required for the queue to be handled correctly. There are several types of queue, namely:

  • Queue (FIFO, First In, First Out)
  • LifoQueue (Last In, First Out)
  • PriorityQueue (in increasing order of value)
  • If you want behavior Last In, First Out, yes.

  • So. That alone makes the implementation of stack separately.

  • 2

    @nbro From what I understand of the cited links, the main difference between using a list as a stack and using a LifoQueue is that the latter is thread-safe - and therefore should be less efficient if you are working with a single thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.