On this page
collections.OrderedDict
Prev: collections.namedtuple | Up: collections | Next: collections.UserDict
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.deque
Ref: docs.python.org — collections.OrderedDict
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import OrderedDict
Signature & Overview
collections.OrderedDict(...)
Dictionary that remembers insertion order
Methods & Attributes
OrderedDict.clear(self, /)— Remove all items from ordered dict.OrderedDict.copy(self, /)— A shallow copy of ordered dict.OrderedDict.fromkeys(iterable, value=None)— Create a new ordered dictionary with keys from iterable and values set to value.OrderedDict.get(self, key, default=None, /)— Return the value for key if key is in the dictionary, else default.OrderedDict.items(self, /)—OrderedDict.itemsmethod/property.OrderedDict.keys(self, /)—OrderedDict.keysmethod/property.OrderedDict.move_to_end(self, /, key, last=True)— Move an existing element to the end (or beginning if last is false).OrderedDict.pop(...)— If the key is not found, return the default if given; otherwise, raise a KeyError.OrderedDict.popitem(self, /, last=True)— Remove and return a (key, value) pair from the dictionary.OrderedDict.setdefault(self, /, key, default=None)— Insert key with a value of default if key is not in the dictionary.OrderedDict.update(...)—OrderedDict.updatemethod/property.OrderedDict.values(self, /)—OrderedDict.valuesmethod/property.
Usage
from collections import OrderedDict
od = OrderedDict([("a", 1), ("b", 2), ("c", 3)])
od.move_to_end("a") # move to right end
assert list(od) == ["b", "c", "a"]
assert od.popitem(last=False) == ("b", 2) # FIFO pop from left
Gotchas
- Plain
dictpreserves insertion order too (Python 3.7+): Reach forOrderedDictspecifically when you needmove_to_end(),popitem(last=False), or order-sensitive equality. - Equality checks key order:
OrderedDict(a=1, b=2) == OrderedDict(b=2, a=1)isFalse, whereas plaindictignores order in==.
REPL Inspect
from collections import OrderedDict
help(OrderedDict)
Linked from
Related notes
- tuts/python3/stdlib_module/collections/namedtuple Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/ChainMap Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserList shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib