In Python 2 you can avoid making a temporary copy of all the values by using the
itervalues() dictionary method, which returns an iterator of the dictionary's keys:
In Python 3 you can just use
d.values() because that method was changed to do that (and
itervalues() was removed since it was no longer needed).
To make it easier to write version independent code which always iterate over the values of the dictionary's keys, a utility function can be helpful:
return iter(getattr(d, ('itervalues', 'values')[sys.version_info>2])())
This is essentially what Benjamin Peterson's
six module does.