Python dictionaries consistency

This is probably a question about the noob. For any dictionary 'd' in python, this is always True:

dict( zip( d.keys(), d.values() ) ) == d 

Are keys and values ​​returned in the same order?

+5
source share
3 answers

Yes, this is always the case. Python is guaranteed if there are no intermediate modifications to ditionary.

Related specification: http://docs.python.org/library/stdtypes.html#dict.items

This is better, as a rule, because it protects against a dict coming out of synchronization and uses a little extra memory:

dict((k,v) for k,v in d.iteritems())

+6
source

, , "". :

items(), keys(), values(), iteritems(), iterkeys() itervalues() , .

, dict( zip( d.keys(), d.values() ) ) == d True , . , d, d.keys(), d.values(), dict(...). , , .

+2

, : -)

, d.items: http://docs.python.org/library/stdtypes.html#dict.items

. d.items() , d.keys() d.values().

+2

All Articles