How would I update OrderedDict's values at certain position (beginning from 0)? So:
od = OrderedDict(a=1, b=2, c=3)
od.update({'b': 4}) # a=1, b=4, c=3
od.update_values(2, 1) # a=2, b=1, c=3
Since dictionaries in Python 3.5 don't remember the order of their items, you don't know the order in the resulting ordered dictionary until the object is created. From this point on, the order is maintained. Since Python 3.6, functions retain the order of keyword arguments passed in a call.
An OrderedDict is a dictionary subclass in which the order of the content added is maintained.
Python 3.6 (CPython) As of Python 3.6, for the CPython implementation of Python, dictionaries maintain insertion order by default.
This is simple - just use od[od.keys()[x]] = y
This works for dictionaries too (although that would be pointless.) The code simply takes a list
of the keys of the orderedDict, then takes the entry at the desired position from the list, and then uses that string as a key for the original orderedDict.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With