Retrieving dictionary keys within with another dictionay - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Retrieving dictionary keys within with another dictionay (/thread-22072.html) |
Retrieving dictionary keys within with another dictionay - bazcurtis - Oct-28-2019 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? RE: Retrieving dictionary keys within with another dictionay - ichabod801 - Oct-28-2019 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. RE: Retrieving dictionary keys within with another dictionay - bazcurtis - Oct-28-2019 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. RE: Retrieving dictionary keys within with another dictionay - ichabod801 - Oct-28-2019 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. RE: Retrieving dictionary keys within with another dictionay - bazcurtis - Oct-29-2019 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'}} RE: Retrieving dictionary keys within with another dictionay - ichabod801 - Oct-29-2019 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) RE: Retrieving dictionary keys within with another dictionay - bazcurtis - Oct-29-2019 Sorry Craig. I understand what you are saying, but I am clear on how to do that. RE: Retrieving dictionary keys within with another dictionay - ichabod801 - Oct-29-2019 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.
RE: Retrieving dictionary keys within with another dictionay - bazcurtis - Oct-29-2019 Thanks Craig, I will take a look. Sorry, I thought you were giving me a hint via my code :) |