This looks almost identical to the list example above. pop () Traceback (most recent call last):įile "", line 1, in Inde圎rror: pop from an empty deque append ( 'c' ) > myStack deque() > myStack. > from collections import deque > myStack = deque () > myStack. You can use the same methods on deque as you saw above for list. deque is pronounced “deck” and stands for “ double-ended queue.” The collections module contains deque, which is useful for creating Python stacks. The next data structure will help you get around the reallocation problem you saw with list. This is not normally something you would do to a stack, however. insert() to add an element to your stack at a position other than the end, it can take much longer. append() calls taking much longer than other ones. If your stack grows bigger than the block of memory that currently holds it, then Python needs to do some memory allocations. At a high level, this means that the items are stored next to each other in memory. The items in a list are stored with the goal of providing fast access to random elements in the list. The biggest issue is that it can run into speed issues as it grows. Unfortunately, list has a few shortcomings compared to other data structures you’ll look at. You know how it works and likely have used it in your programs already. List has the advantage of being familiar. You can see in the final command that a list will raise an Inde圎rror if you call. pop () Traceback (most recent call last):įile "", line 1, in Inde圎rror: pop from empty list pop() on an empty stack in the implementation descriptions below. Hitting Undo again after this will have no effect because your undo stack is empty, at least in most editors. This removes the Delete Word item, leaving only one operation on the stack.įinally, if you hit Undo a third time, then the last item will be popped off the stack: When you hit undo again, the next item is popped off the stack: This operation is the opposite of push and is commonly called pop. Your editor undoes the indent, and the undo stack now contains two items. It takes the item at the top of the stack, which was indenting the comment, and removes that from the stack: Now you’ve decided to undo all three of those changes, so you hit the undo command. When you’re working with stacks, adding new items like this is called push. You can see that each of these commands are stored in an undo stack, with each new command being put at the top. Finally you indent a comment so that it’s lined up properly: Notice how the Delete Word item is placed on top of the stack. After adding the function, you delete a word from a comment. You can see that the stack now has an Add Function operation on it. Let’s imagine you’re editing a Python file so we can look at some of the operations you perform. It’s probably easiest to understand a stack if you think of a use case you’re likely familiar with: the Undo feature in your editor. This is in contrast to a queue, which stores items in a First-In/First-Out (FIFO) manner. What Is a Stack?Ī stack is a data structure that stores items in an Last-In/First-Out manner. Free Bonus: Click here to get a Python Cheat Sheet and learn the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |