How to iterate over a range of keys in a dictionary?

dictionary, python

Solution

The keys in a Python dictionary are not in any specific order.

You'll want to use an OrderedDict instead.

For example:

>>> d = OrderedDict([('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])

Now the keys are guaranteed to be returned in order:

>>> d.keys()
['key1', 'key2', 'key3']

If you want to grab all keys after a specific value, you can use itertools.dropwhile:

>>> import itertools
>>> list(itertools.dropwhile(lambda k: k != 'key2', d.iterkeys()))
['key2', 'key3']

Problem

How do you iterate over a range of keys in a dictionary? for example, if I have the following dictionary: ``` {'Domain Source': 'Analyst', 'Recommend Suppress': 'N', 'Standard Error': '0.25', 'Element ID': '1.A.1.d.1', 'N': '8', 'Scale ID': 'IM', 'Not Relevant': 'n/a', 'Element Name': 'Memorization', 'Lower CI Bound': '2.26', 'Date': '06/2006', 'Data Value': '2.75', 'Upper CI Bound': '3.24', 'O*NET-SOC Code': '11-1011.00'} ``` how would I iterate over only the keys after standard error? Ideally, I would like to get all the values following standard error. Thanks! Just to address the comment: I know about iteritems(), but when I tried subscripting, returned an error: not subscriptable. Also, the key / values come in the same order every time.

Original source