I have a main and blank dictionary,
Dairy_goods = {1:{'item':'Milk','price':2.47,'gst':0.16,'offer':'Yes'},
2:{'item':'Butter','price':4.50,'gst':0.32,'offer':'No',},
3:{'item':'Egg','price':3.40,'gst':0.24,'offer':'No'}}
I have coded it so that it will sort by alphabetical order.
While True:
res = sorted(Dairy_goods.items(), key = lambda x: x[1]['item']) #by name
print("The sorted dictionary by marks is : " + str(res))
the output would be
The sorted dictionary by marks is : [(2, {'item': 'Butter', 'price': 4.5, 'gst': 0.32, 'offer': 'No'}),
(3, {'item': 'Egg', 'price': 3.4, 'gst': 0.24, 'offer': 'No'}),
(1, {'item': 'Milk', 'price': 2.47, 'gst': 0.16, 'offer': 'Yes'})]
As you can see the main key value are messed up after the sort and would like to know if there was a way to change/update the key name so it would be listed in order.
#desired output
The sorted dictionary by marks is : [(1, {'item': 'Butter', 'price': 4.5, 'gst': 0.32, 'offer': 'No'}),
(2, {'item': 'Egg', 'price': 3.4, 'gst': 0.24, 'offer': 'No'}),
(3, {'item': 'Milk', 'price': 2.47, 'gst': 0.16, 'offer': 'Yes'})]
Thank you.
*edit: I realize the title of this post might not fit what I am looking for do let me know how I should title this issue if you know, Thank you
*edit: As AKS has mentioned, using enumerate worked great! but as I would still like the value of the number to be attached to the list, for example when i select option '1' it would still give me the key values of 'Milk' as it is the first key in the main dictionary is there any work around for this?
#output now
[(1, {'item': 'Butter', 'price': 4.5, 'gst': 0.32, 'offer': 'No'}),
(2, {'item': 'Egg', 'price': 3.4, 'gst': 0.24, 'offer': 'No'}),
(3, {'item': 'Milk', 'price': 2.47, 'gst': 0.16, 'offer': 'Yes'})]
select your input: 1 #user input
how many do you want?: 1 #user input
Milk , {'Quantity': 1, 'Individual price': 2.47, 'total': 2.47, 'GST': 0.16, 'offer': 'Yes'} #output