Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Exploring Other Features of Deque
00:00
In this lesson, you’ll learn about other arguments and methods that deque provides, how they work, and how to use them in your code. One of the most useful features of deque is the possibility to specify its maximum length or number of items using the maxlen argument when you’re instantiating the class.
00:19
If you don’t specify a value for maxlen, as you’ve been doing so far, it defaults to None, allowing the deque to grow to an arbitrary number of items.
00:30
If you provide a value to maxlen, then your deque will only store up to maxlen items. When maxlen is provided, we say that it is called a bounded deque.
00:42
Once a bounded deque is full with a specified number of items, adding a new item at either end automatically removes and discards the item at the opposite end.
00:54
This deque can hold at most four items. Since the initial sequence contains five items, the leftmost item 0 is automatically discarded.
01:08
At this point, the deque is already full, so appending 5 to the right requires making room, so the leftmost item 1 is automatically removed.
01:20
Again, the deque at this point is full, so adding 6 removes the leftmost item 2.
01:30
This time, a new item is added to the left side. To maintain the maximum size of 4, the rightmost item 6 is automatically removed.
01:40 If we repeat this process once again, adding an item to the left removes an item from the right.
01:49
The maxlen attribute returns the maximum number of items the deque can hold. This attribute is read-only, so you can inspect its value, but you cannot modify it after the deque has been created.
02:03
Having the option to restrict the maximum number of items allowed in your deque is useful for tracking the latest elements in a given sequence of objects or events.
02:14 For example, you can track the last five transactions in a bank account, the last ten open text files in an editor, and the last five pages in a browser, and so on.
02:26
Another interesting feature of deques is the possibility to rotate their elements by calling .rotate() on a nonempty deque. This method takes an integer n as an argument and rotates the items n steps to the right. The default value of n is 1.
02:45
In other words, it moves n items from the right end to the left end in a circular fashion. If you provide a negative value to n, then the rotation is to the left.
02:58
Like regular lists, deques allow you to add several items to the right end of a deque using an iterable as an argument with the .extend() method.
03:11
deques also have a method called .extendleft(), which takes an iterable as an argument and adds its items to the left end of the target deque in one go.
03:23
Internally, .extendleft() performs a series of individual .appendleft() operations that process the input iterable from left to right.
03:32
This ends up adding the items in reverse order to the left end of the target deque. Next up, you’ll learn about the sequence-like features of deque.
Become a Member to join the conversation.