dict: keys() and values() order guaranteed to be same?

Philipp Hagemeister phihag at phihag.de
Mon Jul 23 13:40:42 CEST 2012

On 07/23/2012 01:23 PM, Henrik Faber wrote:
> With an arbitrary dictionaty d, are d.keys() and d.values()
> guaraneed to be in the same order?

Yes. From the documentation[1]:

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues()
are called with no intervening modifications to the dictionary, the
lists will directly correspond.

In most cases, you should simply use items() though. Can you elaborate
on the use case for needing both keys() and values(), where items() is
not applicable?

- Philipp

[1] http://docs.python.org/library/stdtypes.html#dict.items

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20120723/2709edc6/attachment.sig>

More information about the Python-list mailing list