I have od
of type OrderedDict
. I want to access its most recently added (key, value) pair. od.popitem(last = True)
would do it, but would
God, I wish this was all built-in functionality...
Here's something to save you precious time. Tested in Python 3.7. od
is your OrderedDict.
# Get first key
next(iter(od))
# Get last key
next(reversed(od))
# Get first value
od[next(iter(od))]
# Get last value
od[next(reversed(od))]
# Get first key-value tuple
next(iter(od.items()))
# Get last key-value tuple
next(reversed(od.items()))