The first or last item (but non-destructively) of the OrderedDict that you sometimes want. What is it that you can't index even though you remember the order?
python
>> from collections import OrderedDict
>> od = OrderedDict(((1, 'a'), (2, 'b'), (3, 'c')))
>> od
OrderedDict([(1, 'a'), (2, 'b'), (3, 'c')])
>> next(iter(od))
1
>> next(iter(od.values()))
'a'
>> next(iter(od.items()))
(1, 'a')
python
>> next(reversed(od))
3
The following is omitted.
The source has been forgotten somewhere on stackoverflow.
Recommended Posts