The peek_rear() and peek_front() methods return the value of the next item on either end of the deque but doesn't remove those items from the deque. The video demonstrates how to use simple list indexing to accomplish this.
- [Narrator] Let's code out the logic for our peek_front…and peek_rear methods.…It'll be very similar to the remove_front and remove_rear,…but instead of actually removing the items…we just want to see what they are.…So to do that, we can say Return Self.Items…and we can simply specify the index at which the item exists…that we want to see.…So the front of the Deque again is represented…by the left side of the list.…
So the left most item in the list…is going to be at the 0th index.…Now, it may be the case that we try to call peek_front…on a list that doesn't have anything in it.…So let's account for that now.…And we'll just say as long as there are items in the list,…show me that left most item item.…And if the list is empty, we'll just return None.…Peek_rear is going to be nearly identical,…so all we're going to need to change is that index.…
We'll say, if self.items, meaning if there is something…in the list, return self.items…at the negative first index.…This will always give us the last item in the list.…And if the list is empty, return None.…
- 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 10m 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.