how do i inverse a python dictionary's key, value pair
Body I have a Python dictionary in which the keys represent food names and the values represent their tastes. For example: foods = { "apple": "sweet", "lemon": "sour", "mango": "sweet", "grapefruit": "sour", "banana": "sweet" } I want to organize the food names based on their taste. For example, I would like to get the following output: { "sweet": ["apple", "mango", "banana"], "sour": ["lemon", "grapefruit"] } I initially thought about reversing the dictionary's key-value pairs, but since multiple food items can have the same taste, I need to store multiple keys under the same value. How can I
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.