Python Forum
Retrieving dictionary keys within with another dictionay
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving dictionary keys within with another dictionay
#1
I have a list of dictionaries, each dictionary contains keys relating to its health.

I am creating the list via the following as I don't want ever key in the dictionary.

computer_keys = ('id', 'hostname', 'lastSeenAt', 'threats', 'ProtectionEnabled', 'services', 'ipv4Addresses', 'associatedPerson', 'health')
for all_computers in computers_json["items"]:
    computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
    computer_list.append(computer_dictionary)
The list looks like this

[
{
"associatedPerson": {
"viaLogin": "user"
},
"encryption": {
"volumes": [
{
"status": "notEncrypted",
"volumeId": "60BB3F30"
}
]
},
"health": {
"overall": "good",
"services": {
"serviceDetails": [
{
"name": "service",
"status": "running"
},

The issue I am having is, Health will return the dictionary below it. I just need the overall key, not all others. How can I do this?
Reply
#2
Add a modification after line 3:

computer_dictionary['health'] = {'overall': computer_dictionary['health']['overall']}
That assumes every computer dictionary has a health key. If not, you'll need a condition to check for that or you'll get a KeyError.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks Craig. That worked fine. Following the same logic I then added

computer_dictionary['service_health'] = {'services': computer_dictionary['health']['services']['status']}
I thought this would add new key to the dictionary with the service health.

    computer_keys = ('id', 'hostname', 'lastSeenAt', 'threats', 'service_health', 'tamperProtectionEnabled', 'ipv4Addresses', 'associatedPerson', 'health')
    #Add the computers to the computers list
    for all_computers in computers_json["items"]:
        # Make a temporary Dictionary to be added to the sub estate list
        computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
        if 'health' in computer_dictionary.keys():
            #computer_dictionary['health'] = {'threats': computer_dictionary['health']['threats']['status']}
            computer_dictionary['health'] = {'overall': computer_dictionary['health']['overall']}
            computer_dictionary['service_health'] = {'services': computer_dictionary['health']['services']['status']}
            #computer_dictionary['health'] = {'threats': computer_dictionary['health']['threats']['status']} # seems to work
        computer_list.append(computer_dictionary)
If I comment out this line it works fine

computer_dictionary['health'] = {'overall': computer_dictionary['health']['overall']}
I was thinking I would get the result of each sub key and then make a new key for the result. This will make it easier when I export it to CSV at the end.
Reply
#4
The code I suggested wipes out what was in computer_dictionary['health'] for the simpler dictionary with just 'overall'. If you want to pull something else from computer_dictionary['health'] as it was initially read, and put that into computer_dictionary['service_health'], you need to do that before my line of code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks Craig. That is working perfectly now.

Here is the new code

    for all_computers in computers_json["items"]:
        # Make a temporary Dictionary to be added to the sub estate list
        computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
        if 'health' in computer_dictionary.keys():
            computer_dictionary['service_health'] = {'services': computer_dictionary['health']['services']['status']}
            computer_dictionary['threats'] = {'threats': computer_dictionary['health']['threats']['status']}
            computer_dictionary['health'] = {'overall': computer_dictionary['health']['overall']}
        computer_list.append(computer_dictionary)
The only issue left is, I really only want the bad or good, but I get the below. Is there anyway to tidy this up?

<class 'dict'>: {'id': '116bd3bf-00b9-42b1-bf11-ffc6de819084', 'hostname': 'DC', 'health': {'overall': 'good'}, 'ipv4Addresses': ['192.168.220.22'], 'ProtectionEnabled': True, 'lastSeenAt': '2018-11-14T17:07:28.240Z', 'service_health': {'services': 'good'}, 'threats': {'threats': 'good'}}
Reply
#6
If you just want the values, just assign the values:

for all_computers in computers_json["items"]:
    # Make a temporary Dictionary to be added to the sub estate list
    computer_dictionary = {key:value for key, value in all_computers.items() if key in computer_keys}
    if 'health' in computer_dictionary.keys():
        computer_dictionary['service_health'] = computer_dictionary['health']['services']['status']
        computer_dictionary['threats'] = computer_dictionary['health']['threats']['status']
        computer_dictionary['health'] = computer_dictionary['health']['overall']
    computer_list.append(computer_dictionary)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Sorry Craig. I understand what you are saying, but I am clear on how to do that.
Reply
#8
Look at the code snippet I posted. You were assigning dictionaries: {key: value} pairs. I just took the value out of that expression and assigned it by itself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thanks Craig, I will take a look. Sorry, I thought you were giving me a hint via my code :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 767 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 534 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 2,244 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 2,289 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 2,140 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Adding keys and values to a dictionary giladal 3 2,425 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Problem: Retrieving Form data PythonDev 3 3,024 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  access dictionary with keys from another and write values to list redminote4dd 6 3,160 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 3,603 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,014 Dec-17-2019, 08:00 PM
Last Post: jasonashaw

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020