The size() method returns the length of the stack, which represents the number of items in the stack. Learn how to use Python's built-in len() method to implement this behavior.
- [Narrator] Since we're representing our stack with a list,…in order to figure out how many items are in the stack,…all we need to do is use the list's built-in length method…to find the length of the underlying list.…So, we'll come into our size method here…and really all I need to do is just return the result…of calling that length method.…So, we'll do, basically, find the length of self.items.…I'll save this, and then go back to my terminal…and run the file, interactively, of course.…
And here, I am going to create my own stack object…and before I add anything to it,…I want to check my size method,…make sure I've coded it properly.…So, we'll do my_stack.size, and I expect to get zero.…Yep, that's what I get.…So now, let's push something onto the stack.…We'll do apple again.…Okay, and if I call the size method again,…I expect that it would return one.…
And that's what I get.…So, let's go back to the code here…and we'll add our doc string and runtime note,…as we have been.…So, what does this method do?…This method returns the length of the list…
- 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.