Handling Complex Data Structures

To work with data structure you can drill down to which information you need using these commands to peel it layer by layer  :

1st determine what kind of data :

List :
output.keys()
type(output)
len(output)
Then to see the structure at that level :
for entry in output:
….print(output)
….Break  <— useful on massive outputs.

Then this would return ether a list or a dictionary and you can narrow down your search to make your code.
for entry in output:
….print(output[‘dict_key1’])
….print(output[‘dict_key2’])
This would print out the values of those keys.

When a len of a List is equal to 1 :
len(output)
new_output = output[0]
print(new_ouput)

Dictionary:

When you get to a dictionary, you want to see what keys you have :
print(new_output.keys())
dict_keys([‘keys’, ‘keys’, etc….])
print(new_output[‘keys’]) <— then you need to look at each keys to see what is next.

When you know which key to use you can create a new variable to be able to start from there.
new_output = new_output[‘keys’]

Then you repeat the process until you get what you need.

Simplifying the code :
For every time we did var = var we can make it under the same line:
new_output = output[0][‘keys][‘keys’] <— up to the final key needed
for key_var, value_var in new_output.items():
….print(key_var)
….print(value_var[‘key’])
….print()

Leave a Comment