116
Suppose I have quantities of fruits of different colors, e.g., 24 blue bananas, 12 green apples, 0 blue strawberries and so on. I'd like to organize them in a data structure in Python that allows for easy selection and sorting. My idea was to put them into a dictionary with tuples as keys, e.g.,
{
('banana', 'blue' ): 24,
('apple', 'green'): 12,
('strawberry','blue' ): 0,
# ...
}
or even dictionaries, e.g.,
{
{'fruit': 'banana', 'color': 'blue' }: 24,
{'fruit': 'apple', 'color': 'green'}: 12,
{'fruit': 'strawberry','color': 'blue' }: 0,
# ...
}
I'd like to retrieve a list of all blue fruit, or bananas of all colors, for example, or to sort this dictionary by the name of the fruit. Are there ways to do this in a clean way?
It might well be that dictionaries with tuples as keys are not the proper way to handle this situation.
All suggestions welcome!