I have an OrderedDict
and I would like to iterate over a subset of its elements, from index n
to m
.I can do it the simple way:
from collections import OrderedDict
d = OrderedDict()
for i in range(10):
d[i] = i
n = 3
m = 6
c = 0
for i in d:
if n <= c <= m:
print(d[i])
c += 1
but I was looking for something more compact, similar to slicing for lists:
n = 3
m = 6
l = [i for i in range(10)]
for i in l[n:m+1]:
print(i)
Is there such a mechanism for OrderedDict
?
It depends on how your OrderedDict
is created (n
and m
need to consider the index of the items in d
), but how about this:
d.values()[n:m]