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

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -