Home

Published

- 1 min read

flip key and value in dictionary python

img of flip key and value in dictionary python

The solution for this is noted below

flip key and value in dictionary python

Solution

   # flip key and values
by_name = {"Negative": -1, "Positive": 1, "Neutral": 0, "Mixed": 2}
by_num = {value: key for key, value in by_name.items()}
# ouput = {-1: 'Negative', 1: 'Positive', 0: 'Neutral', 2: 'Mixed'}

Try other methods by searching on the site. That is if this doesn’t work