OrderedDict là một lớp con từ điển ghi nhớ thứ tự mà nội dung của nó được thêm vào, hỗ trợ các phương thức dict thông thường. . Xóa một mục nhập và nhập lại nó sẽ chuyển nó đến cuối.
>>> from collections import OrderedDict
>>> d = {'banana': 3, 'apple':4, 'pear': 1, 'mango': 2}
>>> od=OrderedDict(d.items())
>>> od
OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('mango', 2)])
>>> od=OrderedDict(sorted(d.items()))
>>> od
OrderedDict([('apple', 4), ('banana', 3), ('mango', 2), ('pear', 1)])
>>> t=od.popitem()
>>> t
('pear', 1)
>>> od=OrderedDict(d.items())
>>> t=od.popitem()
>>> t
('mango', 2)