Sorting a list containing dictionary depending on dictionary keys

Consider a dictionary containing list of mobile phone price and rating as below.

data_list = [ {“product_name”: “Sony xperia C3”, “price”: 23500, “ratings”: 3}, {“product_name”: “HTC mini”, “price”: 35000, “ratings”: 1}, {“product_name”: “BlackBerry Z10”, “price”: 19500, “ratings”: 4}, {“product_name”: “Samsung note 4”, “price”: 50000, “ratings”: 5}, {“product_name”: “Iphone 6”, “price”: 65000, “ratings”: 2}

]

Let see how we can sort the dictionaries inside the list:
Basically I will use two python utilitis: Sorted method and lambda utility.

1. Sort on the basis of price.

In [1]: sorted(data_list, key=lambda x:x[‘price’]) # sorted needs a key to sort, here I have given price as key. Out[1]: [{‘price’: 19500, ‘product_name’: ‘BlackBerry Z10’, ‘ratings’: 4}, {‘price’: 23500, ‘product_name’: ‘Sony xperia C3’, ‘ratings’: 3}, {‘price’: 35000, ‘product_name’: ‘HTC mini’, ‘ratings’: 1}, {‘price’: 50000, ‘product_name’: ‘Samsung note 4’, ‘ratings’: 5},

{‘price’: 65000, ‘product_name’: ‘Iphone 6’, ‘ratings’: 2}]

150 150 Burnignorance | Where Minds Meet And Sparks Fly!