from collections import OrderedDict
d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
o = OrderedDict(d) # OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
# insert item
o['pineapple'] = 10 # OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2), ('pineapple', 10)])
o['apple'] # 4
list(o.keys()) # ['banana', 'apple', 'pear', 'orange', 'pineapple']
list(o.values()) # [3, 4, 1, 2, 10]
list(o.items()) # [('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2), ('pineapple', 10)]
# update
o['apple'] = 10
# empty
#o.clear()
d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
o = OrderedDict(d) # OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
item = o.popitem(last = True) # default, return the last item in LIFO order
print(o, item) # OrderedDict([('banana', 3), ('apple', 4), ('pear', 1)]) ('orange', 2)
# item = o.popitem(last = False) # return the first item in FIFO order
# print(o, item) # OrderedDict([('OrderedDict([('OrderedDict([('OrderedDict([('apple', 4), ('pear', 1), ('orange', 2)]) ('banana', 3)
d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
o = OrderedDict(d) # OrderedDict([('banana', 3), ('apple', 4), ('pear', 1), ('orange', 2)])
o.move_to_end('apple', last = True) # default, the item is moved to the right end
print(o) # OrderedDict([('banana', 3), ('pear', 1), ('orange', 2), ('apple', 4)])
# o.move_to_end('apple', last = False) # default, the item is moved to the left end
# print(o) # OrderedDict([('apple', 4), ('banana', 3), ('pear', 1), ('orange', 2)])