Access data in nested Python lists and dicts
When working with JSON based RESTAPIs, it is common to receive responses with nested data structues in various combinations.
In Python these can easily be iterated through thanks to the built-in flexibility of the for-loop in Python, or they can be accessed directly with list indices or key references.
The following examples serve as a basic reference to working with nested data structures using lists and dictionaries.
In the examples below you can see various methods on accessing data with the following nested formats:
list of lists
list of dicts
dict of lists
dict of dicts
With each of those data structures you will see how to:
loop through keys and/or values
reference elements directly without looping
extract subsections of a list
simple error handling for invalid dict key
Not all of the examples are perfect for every situation, so it's up to you to evaluate what method makes most sense in various cases.