If you have control over how data is aggregated, it is better to have a list of dicts , and IP will be the value inside the data dict {..., 'ip': '127.0.0.1'} , and not the key in the parent type of the container
Convert to a container that preserves the order of elements
You can sort a structure that maintains the order of elements, for example list for example. there is a dict implementation that supports order, for example, OrderedDict , for example.
You can always convert them (maybe this is not your first choice if slow / big data)
Convert to list [(key, value), ...] or list [value, ...]
A possible way is to get all the values ββin a dict and then return a list of these values, sorted of your choice.
You can also sort (key, value) returned by ips_data.items() , but about to create a new list. key is IP and value is IP data.
sorted_list_of_keyvalues = sorted(ips_data.items(), key=item[1]['data_two'])
The list above is in the form [(key1, value1), (key2, value2), ...]
You can also pull out values ββand delete keys
sorted_list_of_values = [item[1] for item in sorted_list_of_keyvalues]
This list is in the form [value1, value2, ...]
Please note that you might think that you can only sort the value, not (the key value), but your data has an IP address in them, and you might want to keep this.
Convert to OrderedDict
If you absolutely want to keep the structure as a dict, you can use OrderedDict
from collections import OrderedDict ordered_items = sorted(ips_data.items(), key=lambda item: item[1]['data_two']) ordered_ips_data_dict = OrderedDict(ordered_items)
An ordered dict behaves exactly the same as a dict, but iterations of keys and elements will maintain the order of the elements.
Or save the sorted list of keys and process in that order
Or, alternatively, you can sort the keys of this dict in a list, then you can process the dict in that order. The advantage is that you do not need to copy / convert data
>>> ips_data = { ... "1.2.3.4": { ... "data_one": 1, ... "data_two": 8, ... "list_one": [], ... "list_two": [ ... "item_one" ... ], ... "data_three": "string1" ... }, ... "5.6.7.8": { ... "data_one": 1, ... "data_two": 9, ... "list_two": [ ... "item_one" ... ], ... "data_three": "string1", ... "data_four": "string2", ... "data_five": "string3" ... } ... } >>> ips_data.keys() ['1.2.3.4', '5.6.7.8'] >>> ips = ips_data.keys()
Now you can sort the keys by the data_two field
>>> sorted_ips = sorted(ips, key=lambda ip: ips_data[ip]['data_two'], reverse=True) >>> sorted_ips ['5.6.7.8', '1.2.3.4']
After sorting the keys, you can do what you want in your sorted key order, for example. processing it in this order may be more efficient than copying a dict into a new structure such as a list
# Trivial example of processing that just puts the values into a list >>> [ips_data[ip] for ip in sorted_ips] [{'data_three': 'string1', 'data_two': 9, 'data_five': 'string3', 'data_four': 'string2', 'list_two': ['item_one'], 'data_one': 1}, {'list_two': ['item_one'], 'data_two': 8, 'data_one': 1, 'data_three': 'string1', 'list_one': []}] >>>