damon
damon

Reputation: 8467

using zip on dict keys and values

I am a bit confused by this paragraph in python docs for dict class

If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys())

what is meant by called with no intervening modifications ?

if I receive a dict instance which was spewed out by some function(I have no way of knowing if the elements were modified since the dict was created)..can I still use the zip(d.values(),d.keys()) ?

Upvotes: 4

Views: 7750

Answers (1)

Lauritz V. Thaulow
Lauritz V. Thaulow

Reputation: 50995

Yes.

The point is you should not modify d between calling d.values() and d.keys().

Upvotes: 6

Related Questions