Python dictionary sort list with null values ββat the end
I have a homogeneous list of null-valued dictionaries, but it can contain any value types. Example:
values=[{'buy_qty': 15, 'product_id': 30}, {'buy_qty': 0,'product_id': 33},{'buy_qty': 25, 'product_id': 50}, {'buy_qty': 7, 'product_id': 22}]
Is there a way, without reinventing the wheel, to get the list sorted by the "minimum" buy_qty "usually for the python path, but the" null "values ββat the end of the list, for example:
values=[{'buy_qty': 7, 'product_id': 22}, {'buy_qty': 15, 'product_id': 30}, {'buy_qty': 25, 'product_id': 50}, {'buy_qty': 0,'product_id': 33}]
I tried with itemgetter,
sorted(total_lines, key=itemgetter('buy_qty'))
It seems to me that there might be some trick here with the "key" parameter
+3
source to share
3 answers