Collections
Collections
Module that contains a number of container data types:
defaultdict
OrderedDict
counter
deque
namedtuple
enum.Enum
(outside of the module; Python 3.4+)
defaultdict
defaultdict
Unlike dict
, with defaultdict
you do not need to check whether a key is present or not:
One other very important use case is when you are appending to nested lists inside a dictionary. If a key
is not already present in the dictionary then you are greeted with a KeyError
. defaultdict
allows us to circumvent this issue in a clever way. First let me share an example using dict
which raises KeyError
and then I will share a solution using defaultdict
.
Problem:
Solution:
You can print some_dict
using json.dumps
. Here is some sample code:
OrderedDict
OrderedDict
Keeps its entries sorted as they are initially inserted.
Overwriting a value of an existing key doesn’t change the position of that key.
Deleting and reinserting an entry moves the key to the end of the dictionary.
Problem:
Solution:
counter
counter
Allows us to count the occurrences of a particular item.
Example: count the number of individual favorite colors:
Example: Count the most common lines in a file using it:
deque
deque
Provides a Double Ended QUEUE, so you can append and delete elements from either side of the queue.
It works like python lists and provides you with somewhat similar methods. Example:
You can pop values from both sides of the deque:
Limit the amount of items a deque can hold: when we achieve the maximum limit of our deque it will simply pop out the items from the opposite end.
Example:
Now whenever you insert values after 5, the leftmost value will be popped from the list. You can also expand the list in any direction with new values:
namedtuple
namedtuple
Immutable list which allows you to store a sequence of values separated by commas.
They are just like lists but have a few key differences. The major one is that unlike lists, you can not reassign an item in a tuple.
In order to access the value in a tuple you use integer indexes like:
namedtuplest
turn tuples into convenient containers for simple tasks. You don’t have to use integer indexes for accessing members of a tuple. You can think of namedtuples like dictionaries but unlike dictionaries they are immutable.
We can access members of a tuple just by their name using a .
.
A named tuple has two required arguments. They are the tuple name and the tuple field_names. In the above example our tuple name was ‘Animal’ and the tuple field_names were ‘name’, ‘age’ and ‘type’. Namedtuple makes your tuples self-document.
As you are not bound to use integer indexes to access members of a tuple, it makes it more easy to maintain your code.
As namedtuple
instances do not have per-instance dictionaries, they are lightweight and require no more memory than regular tuples. This makes them faster than dictionaries.
Attributes in namedtuples are immutable. This would not work:
You should use named tuples to make your code self-documenting. They are backwards compatible with normal tuples. It means that you can use integer indexes with namedtuples as well:
You can convert a namedtuple to a dictionary:
enum.Enum
(Python 3.4+)
enum.Enum
(Python 3.4+)Available in the enum
module, in Python 3.4 and up.
Let’s consider the Animal namedtuple from the last example. It had a type
field. The problem is, the type was a string. This poses some problems for us. What if the user types in Cat
because they held the Shift key? Or CAT
? Or kitten
?
Enumerations can help us avoid this problem, by not using strings. Example:
This is much less error-prone. We have to be specific, and we should use only the enumeration to name types.
3 ways to access enumeration members. Example:
Last updated