The pop() method removes and returns the topmost item of the stack. In this video, learn how to use the Python list's built-in pop() method to implement this behavior.
- [Instructor] The second method we can work on…is our Pop Method.…And this is the method that's going to allow us…to remove an item from the top of the stack.…Now, we know already that the list data type…has a built in Pop Method.…So, we're just going to make use of that.…So, we will return whatever value…is given to us by self.items.pop.…Now, we don't have to specify an index here,…because we always are going to be wanting it…to return us the last item from the list.…
That's how the Pop Method works.…If you don't give it any parameters,…it will always return the last method.…That's pretty much it.…Let's add another doc string, as we did for Push.…So, what does this method do?…This method…returns the last item.…We should say, removes and returns the last item…from the list,…which is also…the top item of the stack.…
And just as we did before, we should talk about…the run time here, as well.…And the run time is also constant time,…just as it was for Push.…Because really, all we're doing is, we are indexing…to that last item in the list and then returning it.…
- Abstract types and data structures
- Stacks as a linear abstract data type
- Creating the Stack class and its methods
- Adding items to the top and bottom of a stack
- Creating the Queue class and its methods
- Manipulating items in a queue
- Creating the Dequeue class and its methods
- Adding and removing items from a dequeue
Skill Level Advanced
Learning the Python 3 Standard Librarywith Kathryn Hodge2h 9m Intermediate
What you should know1m 26s
1. Abstract Data Types and Data Structures
2. The Stack
3. The Queue
4. The Deque
Next steps1m 43s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.