Get the first 100 elements of OrderedDict
Here's a simple solution using itertools
:
>>> import collections
>>> from itertools import islice
>>> preresult = collections.OrderedDict(zip(range(200), range(200)))
>>> list(islice(preresult, 100))[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
This returns only keys. If you want items, use iteritems
(or just items
in Python 3):
>>> list(islice(preresult.iteritems(), 100))[-10:]
[(90, 90), (91, 91), (92, 92), (93, 93), (94, 94), (95, 95), (96, 96), (97, 97), (98, 98), (99, 99)]
You can slice the keys of OrderedDict and copy it.
from collections import OrderedDict
a = OrderedDict()
for i in xrange(10):
a[i] = i*i
b = OrderedDict()
for i in a.keys()[0:5]:
b[i] = a[i]
b is a sliced version of a