Assume that productDict is a dictionary of product price pairs EX: { 'rice':10.99 'cheese':3.99 'eggs':5.99 } Return a list of tuples of all the products in the dict sorted by price from greatest to least. code example

Example 1: sort dictionary by value python

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

Example 2: sort dictionary

#for dictionary d
sorted(d.items(), key=lambda x: x[1]) #for inceasing order
sorted(d.items(), key=lambda x: x[1], reverse=True) # for decreasing order
#it will return list of key value pair tuples