Is this {'': [ {}, {}, {} ]} Dictionary of List of a Dictionary? How can I Split the inside values so I can print/process them separately?

Question:

I understand I really need Dictionary and List Comprehension understanding. Spent so much time reading all the articles here in stack overflow and else where but getting no where and more confused. Please someone help.

When I query database, basically I get a <class 'dict'> object back of {'': [ {x1:1, y1:2, z1:3}, {...}, {...} ]} syntax. e.g:

{'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}

I’m trying to get those x’s, y’s and z’s value out separately so I can print them nicely or process them as needed. So something like this

1. 'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'

2. 'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'

3. 'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'

There are folks with solution using Pandas/Numpy, Zip() functions, dreaded 🙂 Lambda() function etc… If you can help with simpler solution first that will be great. I’ll understand issue better that way.

Here is sample code that I am testing with.

def query_database():
    result = {'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}
    return result


def display_data():
    result = query_database()
    print("{0}".format(result))

    for item in result:
        print(item)
        print(f">>> {result.values()}")


def main():
    display_data()

if __name__ == '__main__':
    main()
Asked By: aliasSYED

||

Answers:

Using your example data you can just iterate through results key and print whatever you like:

results = {'results': [{'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}, {'Event Time': '2023-01-25T00:18:32.3900000', 'AccountID': 'MICROSOFT\Norma.Cayshun', 'AuditEventMessage': 'User MICROSOFT\Norma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.'}, {'Event Time': '2023-01-16T01:55:05.1930000', 'AccountID': 'MICROSOFT\Christopher.Cheef', 'AuditEventMessage': 'User MICROSOFT\Christopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].'}]}
for row in results['results']:
    print(', '.join([f'{header}: {value}' for header, value in row.items()]))  
Event Time: 2023-01-25T00:18:32.3900000, AccountID: MICROSOFTNorma.Cayshun, AuditEventMessage: User MICROSOFTNorma.Cayshun has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].
Event Time: 2023-01-25T00:18:32.3900000, AccountID: MICROSOFTNorma.Cayshun, AuditEventMessage: User MICROSOFTNorma.Cayshun acknowledged alert SOUTHBAY-VM2 13.107.136.45 has rebooted..
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFTChristopher.Cheef, AuditEventMessage: User MICROSOFTChristopher.Cheef has changed alert note Server Node Down on NORTHLOBBY-VM5 [].
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFTChristopher.Cheef, AuditEventMessage: User MICROSOFTChristopher.Cheef acknowledged alert Server Node Down on NORTHLOBBY-VM5.
Event Time: 2023-01-16T01:55:05.1930000, AccountID: MICROSOFTChristopher.Cheef, AuditEventMessage: User MICROSOFTChristopher.Cheef has changed alert note SOUTHBAY-VM2 13.107.136.45 has rebooted. [].

That being said, if you are going to do anything downstream in python with this data, I would toss the results values into a dataframe

import pandas as pd 
df = pd.DataFrame(results['results'])
display(df)
+-----+-----------------------------+-----------------------------+---------------------------------------------------+
| idx |         Event Time          |          AccountID          |                 AuditEventMessage                 |
+-----+-----------------------------+-----------------------------+---------------------------------------------------+
|   0 | 2023-01-25T00:18:32.3900000 | MICROSOFTNorma.Cayshun     | User MICROSOFTNorma.Cayshun has changed alert... |
|   1 | 2023-01-25T00:18:32.3900000 | MICROSOFTNorma.Cayshun     | User MICROSOFTNorma.Cayshun acknowledged aler... |
|   2 | 2023-01-16T01:55:05.1930000 | MICROSOFTChristopher.Cheef | User MICROSOFTChristopher.Cheef has changed a... |
|   3 | 2023-01-16T01:55:05.1930000 | MICROSOFTChristopher.Cheef | User MICROSOFTChristopher.Cheef acknowledged ... |
|   4 | 2023-01-16T01:55:05.1930000 | MICROSOFTChristopher.Cheef | User MICROSOFTChristopher.Cheef has changed a... |
+-----+-----------------------------+-----------------------------+---------------------------------------------------+

Actual display(df) output in vscode, with jupyter addon, and solarized scheme:

enter image description here

Answered By: JNevill
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.