Deque Objects: Further on Python Collections

Reading further into the collections library of python I came across a new type of object: the deque. Pronounced ‘deck’, deque objects act similarly to lists. Where a list is a collection of indexable elements in Python that are considered immutable, deques allow even further manipulation. For example, a deque can add or subtract elements from either end of its collection. This is actually where deque get their name: double ended queue. Unlike a list which can only take objects from the tail end of its collection. That being said, many methods associated with lists can be performed on deques; just not the other way around. We can match identity, determine length, index and assign to a deque just like a list. In a deque, extend left can be used to assign elements to the end of the deque just like append left can be used to assign a single element. Doing so however will insert the elements in reverse order for the deque, and forward order for the list. Removing an element can be performed with pop whereas removing an element from the front can be achieved with pop left. Unlike lists, elements can be removed from both ends of the deque at the same time without losing data. One final distinction from lists but perhaps one of the most useful features of deques is the ability to rotate elements from the front to the back and vice versa. Rotate will move elements forward and rotate left will move elements backwards.

It is clear from my experience, now that I have graduated, that having a better understanding of object oriented programming. Fortunately, since that time, I have learned that a programmer does not necessarily have to reinvent the wheel. One of the best ways to go about coding, actually, isn’t to code at all. It is to import from previously constructed libraries. By so doing, we save time and frustration. It seems that Collections is one of the biggest modules imported when it comes to simple programming. And it is clear to see why. Collections allows us to iterate over objects, or use collections in much more intuitive or advanced ways than simple methods.