0

Could i get some guidance on how to do this.

Variable

people = {'adam': {'distance': 14, 'age': 22, 'height': 1.3}, 'charles': {'distance': 3, 'age': 37, 'height': 1.4}, 'jeff': {'distance': 46, 'age': 42, 'height': 1.6}}

My Intended output after sorting the people variable by the subkey 'distance'

people = {'charles': {'distance': 3, 'age': 37, 'height': 1.4}, 'adam': {'distance': 14, 'age': 22, 'height': 1.3}, 'jeff': {'distance': 46, 'age': 42, 'height': 1.6}}
  • Creating a *new* dict based the sorted key/value pairs of the original list is fairly simple. Reordering the keys of an existing `dict` is a bit tedious. – chepner Jul 20 '21 at 16:55
  • 2
    Does this answer your question? [Sorting Python dictionary based on nested dictionary values](https://stackoverflow.com/questions/11753660/sorting-python-dictionary-based-on-nested-dictionary-values) – Boris Verkhovskiy Jul 20 '21 at 16:57
  • `OrderedDict` is mostly obsolete as of Python 3.6. See [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/q/39980323/5987) – Mark Ransom Jul 20 '21 at 17:31

3 Answers3

4

Most answers provide a way to create a new dictionary based the contents of the old. If you want to simply reorder the keys of the existing dictionary, you can do something similar:

for k in sorted(people, key=lambda x: people[x]['distance']):
    people[k] = people.pop(k)

When a key is removed, it is also removed from the iteration order. Adding it back makes it the last key in the iteration order. Repeat this for every key, and you redefine the iteration order of the keys. This works because sorted completes its iteration over the dict before the for loop starts modifying it.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • hmm. @chepner This is really interesting! Thanks! Space vs time considerations would come into play at this point vs the accepted answer i guess. I cant say which solution is the better tbh (vs the accepted) so i'll let others weigh in, though both would solve my problem. – Sopeade Lanlehin Jul 20 '21 at 17:14
  • Time should be comparable. Memory usage is probably comparable. The main difference is if, for example, you expect to pass a dict to a function and have it sorted after the function returns, like `inplace_dict_sort(d)`. – chepner Jul 20 '21 at 17:16
2

Just use sorted()

people = dict(sorted(people.items(), key=lambda x: x[1]['distance']))

or

people = {k: v for k, v in sorted(people.items(), key=lambda x: x[1]['distance'])}
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
0

try the following code:

people = {'adam': {'distance': 14, 'age': 22, 'height': 1.3}, 'charles': {'distance': 3, 'age': 37, 'height': 1.4}, 'jeff': {'distance': 46, 'age': 42, 'height': 1.6}}
people = dict(sorted(people.items(), key=lambda item: item[1]['distance'], reverse=False))
print(people)

Output:

people = {'charles': {'distance': 3, 'age': 37, 'height': 1.4}, 'adam': {'distance': 14, 'age': 22, 'height': 1.3}, 'jeff': {'distance': 46, 'age': 42, 'height': 1.6}}
Pranta Palit
  • 663
  • 2
  • 5
  • 15