Convenience and performance with Python collections
17 Jan 2016The collections library included with python has some very helpful utilities to make your programming life a little easier. In today’s post, I’m going to go through a few of them.
Named tuple
This is really the feature that brought my attention to this library, initially. Where a tuple is an immutable set of values that are unnamed, you can create a class using the namedtuple() function to bring a little more formality to your types:
That’s a neat shortcut.
Counter
A Counter class is a dict object that when queried for a key that doesn’t exist, will return a 0; and create that item ready for counting.
Pretty handy.
deque
A basic stack or queue like data structure can be initialized with the use of the deque class. As this object does look a lot like a list
, it’s important to remember why it exists:
Though
list
objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs forpop(0)
andinsert(0, v)
operations which change both the size and position of the underlying data representation.
This tells us that some internal implementation assumptions have been made to tailor the runtime usecase of this class specifically for statically sized queues.