Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python appending to List from a nested Dictionary

I have a nested dictionary like the example below:

dev_dict = {
    "switch-1": {"hostname": "switch-1.nunya.com", "location": "IDF01"},
    "switch-2": {"hostname": "switch-2.nunya.com", "location": "IDF02"},
    "...": {"hostname": "...", "location": "..."},
    "switch-30": {"hostname": "switch-30.nunya.com", "location": "IDF30"},
    "router-1": {"hostname": "router-a-1.nunya.com", "location": "MDF"},
    "core-1": {"hostname": "core-1.nunya.com", "location": "MDF"},
    "...": {"hostname": "...", "location": "..."},
}

I’m appending the dictionaries to a list using this code:

dev_list = []
for i in dev_dict:
    dev_list.append(dev_dict[i])

Which generates a list like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dev_list = [
    {"hostname": "switch-30.nunya.com", "location": "IDF30"},
    {"hostname": "core-1.nunya.com", "location": "MDF"},
    {"hostname": "switch-2.nunya.com", "location": "IDF02"},
    {"hostname": "...", "location": "..."},
    {"hostname": "router-1.nunya.com", "location": "MDF"}
    {"hostname": "...", "location": "..."},
]

What I would like to accomplish is to have the list that’s generated be in a certain order based on the location‘s key value.

The order I’d like it to be is if the location is in the MDF then append those first, then if the location is in an IDF append those to the list after the MDF but in ascending order. So the final list would look like this:

[
    {"hostname": "router-1.nunya.com", "location": "MDF"},
    {"hostname": "core-1.nunya.com", "location": "MDF"},
    {"hostname": "...", "location": "..."},
    {"hostname": "switch-1.nunya.com", "location": "IDF01"},
    {"hostname": "switch-2.nunya.com", "location": "IDF02"},
    {"hostname": "...", "location": "..."},
    {"hostname": "switch-30.nunya.com", "location": "IDF30"},
]

How can I modify my code to accomplish this?

>Solution :

Try this

# add a white space before MDF if location is MDF so that MDF locations come before all others
# (white space has the lowest ASCII value among printable characters)
sorted(dev_dict.values(), key=lambda d: " MDF" if (v:=d['location'])=='MDF' else v)

# [{'hostname': 'router-a-1.nunya.com', 'location': 'MDF'},
#  {'hostname': 'core-1.nunya.com', 'location': 'MDF'},
#  {'hostname': '...', 'location': '...'},
#  {'hostname': 'switch-1.nunya.com', 'location': 'IDF01'},
#  {'hostname': 'switch-2.nunya.com', 'location': 'IDF02'},
#  {'hostname': 'switch-30.nunya.com', 'location': 'IDF30'}]

Click here to see the complete ASCII table.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading