The push() method adds an item to the top of the stack. This video shows how to use the Python list's built-in append() method to accomplish this.
- [Female Instructor] The first method we can flush out…is this push method…and this is the method that's going to allow us…to add an item onto the stack.…We've already specified the item here…and so now we just need to append that…to the list that we've created above.…So we'll just say self dot items dot append…and we'll pass in the item that we want to add.…Now since this is a learning exercise,…I do think it's a good idea that we add doc strings…to each of these methods.…
So I'm just going to write a brief note about…what this method does.…So I'll say, it accepts an item as a parameter,…and appends it to the end of our list.…And it returns nothing.…Now we talked earlier about how…appending to the end of a list happens in constant time.…So let's make a note about the run time as well…and the reason I think it's important to do this is…data structures are often covered in interviews…and typically,…your interviewer will ask you about the run time.…
So I'm just going to say that the run time for this method…is 0(1), or constant time,…
- 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.