Page 335 - Beginning Programming with Pyth - John Paul Mueller
P. 335

print(Item, end=" ")
The implementation of deque is found in the collections package, so you need to import it into your code. When you create a deque, you can optionally specify a starting list of iterable items (items that can be accessed and processed as part of a loop structure) and a maximum size, as shown.
A deque differentiates between adding one item and adding a group of items. You use append() or appendleft() when adding a single item. The extend() and extendleft() functions let you add multiple items. You use the pop() or popleft() functions to remove one item at a time. The act of popping values returns the value popped, so the example prints the value onscreen. The remove() function is unique in that it always works from the left side and always removes the first instance of the requested data.
Unlike some other collections, a deque is fully iterable. This means that you can obtain a list of items by using a for loop whenever necessary.
2. Click Run Cell.
Python outputs the information shown in Figure 14-18 (the
screenshot shows only the output and none of the code).
Following the output listing closely is important. Notice how the size of the deque changes over time. After the application pops the j, the deque still contains eight items. When the application appends and extends from the left, it adds three more items. However, the resulting deque contains only ten items. When you exceed the maximum size of a deque, the extra data simply falls off the other end.
   


























































































   333   334   335   336   337