reversing dictionary in python doesn't work -


this code switching between key , value in dictionary:

for k in d:     d[d[k]] = k     d.pop(k) 

the problem code makes more operations should make, , inputs return wrong result

what reason of problem?

the problem you're changing dictionary while iterating on it, , knows problems causes.

note speaking can't reverse dictionaries: keys must unique , hashable, values don't have be. i'm assuming dictionaries have unique hashable values it's @ least possible.

then use dictionary comprehension create new dictionary, , assign that:

d = {value: key (key, value) in d.items()} 

Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -