Talking about an index
of a dictionary doesn't make much sense. If you want an index, you should make a list. You can make a list of values()
and sort that with a key. For example:
d = {1:('RYAN',25),2:('WILL',12),3:('JOHN',1),4:('MATT',15),5:('FILL',19),6:('NICK',4)}
list(sorted(d.values(), key = lambda x: x[1]))
Result:
[('JOHN', 1),
('NICK', 4),
('WILL', 12),
('MATT', 15),
('FILL', 19),
('RYAN', 25)]
If you really need a dict, you can pass this list back to OrderedDict()
(or dict()
in newer versions of python that preserve order), but you won't be able to index it with zero or -1 unless you pull out the values or keys and convert those to a list:
from collections import OrderedDict
d = {1:('RYAN',25),2:('WILL',12),3:('JOHN',1),4:('MATT',15),5:('FILL',19),6:('NICK',4)}
od = OrderedDict(sorted(d.values(), key = lambda x: x[1]))
od.keys()
# odict_keys(['JOHN', 'NICK', 'WILL', 'MATT', 'FILL', 'RYAN'])