try:
    return iter(d.items())
except AttributeError:
    return list(d.items())
d = dict(red=1, blue=2, green=3)
for k, v in iteritems(d):
    print(k)
print(v)
print('')
Generators to iterate over a dictionary uniformly in both Python 2 and 3