collections – Container datatypes

This module implements a subset of the corresponding CPython module, as described below. For more information, please refer to the original CPython documentation: collections

This module implements advanced collection and container types to hold/accumulate various objects.

Classes

collections.deque(iterable, maxlen[, flags])

Deques (double-ended queues) are a list-like container that supports O(1) appends and pops from either side of the deque. Create new deques with the following arguments:

  • iterable must be an empty tuple, and the new deque is created empty.

  • maxlen must be specified and limits the deque to this maximum length. Once the deque is full, any new items added will discard items from the opposite end.

  • When adding items, the optional flags can be 1 to check for overflow.

In addition to supporting bool, len, iteration and subscript load and store, deque objects have the following methods:

deque.append(x)

Add x to the right side of the deque. If overflow checking is enabled and there is no remaining space, an IndexError is raised.

deque.appendleft(x)

Add x to the left side of the deque. If overflow checking is enabled and there is no remaining space, an IndexError is raised.

deque.pop()

Remove and return an item from the right side of the deque. If no items are present, an IndexError is raised.

deque.popleft()

Remove and return an item from the left side of the deque. If no items are present, an IndexError is raised.

deque.extend(iterable)

Extend the deque by appending all items from iterable to the right side of the deque. If overflow checking is enabled and there is no remaining space, an IndexError is raised.

collections.namedtuple(name, fields)

This is a factory function for creating a new namedtuple type with a specific name and set of fields. A namedtuple is a subclass of tuple that allows accessing its fields not only by numeric index, but also with an attribute access syntax using symbolic field names. The fields are a sequence of strings specifying the field names. For compatibility with CPython, it can also be a string with space-separated field names (but this is less efficient).

Usage example:

from collections import namedtuple

MyTuple = namedtuple("MyTuple", ("id", "name"))
t1 = MyTuple(1, "foo")
t2 = MyTuple(2, "bar")
print(t1.name)
assert t2.name == t2[1]
collections.OrderedDict(...)

A subclass of the dict type that remembers and retains the order of the added keys. When iterating over an OrderedDict, the keys/items are returned in the order they were added:

from collections import OrderedDict

# To make benefit of ordered keys, OrderedDict should be initialized
# from sequence of (key, value) pairs.
d = OrderedDict([("z", 1), ("a", 2)])
# More items can be added as usual
d["w"] = 5
d["b"] = 3
for k, v in d.items():
    print(k, v)

Output:

   z 1
   a 2
   w 5
   b 3

.. method:: OrderedDict.popitem()

   Remove and return a (key, value) pair from the dictionary. Pairs are returned in LIFO (last-in, first-out) order.

   .. admonition:: Differences from CPython
      :class: attention

      OrderedDict.popitem() does not support the last=False argument, and if present, will always remove and return the last item.
      The workaround is to use pop(<first_key>) to remove the first item:

      ::

          first_key = next(iter(d))
          d.pop(first_key)