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
#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


Messages In This Thread
RE: Retrieving dictionary keys within with another dictionay - by bazcurtis - Oct-28-2019, 04:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Retrieving the filename from a shell jehoshua 3 883 Nov-29-2024, 01:25 AM
Last Post: jehoshua
  KeyError while retrieving ESPN data john317ab 2 2,210 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 1,426 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 4,345 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 4,353 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 3,044 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Adding keys and values to a dictionary giladal 3 3,637 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Problem: Retrieving Form data PythonDev 3 4,357 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  access dictionary with keys from another and write values to list redminote4dd 6 4,655 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 5,437 May-30-2020, 11:39 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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