The add_rear() and add_front() methods add items to either side of the deque. This video shows how to use the Python list's built-in insert() append() methods to accomplish this behavior.
- [Instructor] It's time to start coding the logic…for all of these new methods…that we've stubbed out for our deque class.…Let's start with the methods…that have us adding items into the deque.…We can start with add front.…Remember that we're using the left side of the list…to represent the front of the deque.…So when we add an item to the list…we're going to be inserting it…into the zeroth index of the list.…This is exactly how you would in-queue an item onto a queue.…
So we'll use the exact same logic.…For add front, we'll do self.items.insert.…And here we need to pass in both the index…and the item that we want to insert.…For adding an item to the rear of the deque,…which is represented by the right side of the list,…we simply need to append the item…we want to add onto the list.…For that we can do self.items.append…and pass in the item like that.…
I'll save this file and go to the terminal,…where I'll start the interpreter.…And I've chosen to name this Python file deques.py.…First thing I need to do is create my own deque object,…
- 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.