
Problem –
Let’s say we have some fruits prices data.
In [33]: prices = {'banana': 20,
...: 'apple': 120,
...: 'orange':80,
...: 'papaya':30,
...: 'strawberry':130}
And we want to find Maximum or Minimum prices of the fruit along with their name from this dictionary.
Solution –
To get the maximum and minimum price along with the fruit names we can invert the keys and values of the dictionary using the zip function.
In [35]: min_price = min(zip(prices.values(), prices.keys()))
In [36]: min_price
Out[36]: (20, 'banana')
In [37]: max_price = max(zip(prices.values(), prices.keys()))
In [38]: max_price
Out[38]: (130, 'strawberry')