Lesson 6: Nested Data

Nested data is simply data structures that make saving, collecting, accessing, accumulating data more sensible sometimes.

It’s not a very complicated topic and nested data usually rather simplifies the representation and handling of data. We will go through some of the most common nested data examples in this lesson.

  • List outside
    • List inside
    • Dictionary inside


  • Dictionary outside
      • List inside
      • Dictionary inside

Let’s see some examples.

Function : N/A

No new function will be introduced in this lesson.

Used Where?

Nested Data can make accessing, accumulating, collecting, parsing, saving, reading data more sensible.

You can come across nested data situations in very simple data form such as small sized lists in your program or you can also encounter more complex or bigger size examples in web and computer applications such as:

  • APIs
  • Databases
  • Other data collection tools
  • Softwares
  • Games

Syntax do(s)

1) Simply data syntax is followed whether its lists or dictionaries involved. And as always strings are represented inside quotes.

Syntax don't(s)


Lists inside a list:

Example 1

>>> nested_lst1 = [[1,2], [“Venus”,”Mars”], [True, False]]
>>> print(nested_lst)

[[1, 2], [‘Venus’, ‘Mars’], [True, False]]

Accessing a nested list:

Example 2

>>> nested_lst1 = [[1,2], [“Venus”,”Mars”], [True, False]]
>>> print(nested_lst[1])

[‘Venus’, ‘Mars’]

Accessing inner elements:

Example 3

>>> nested_lst1 = [[1,2], [“Venus”,”Mars”], [True, False]]
>>> print(nested_lst[1][1])


Dictionaries inside a list:

Example 4

>>> nested_lst2 = [{“camera”:2, “phone“:1}, {“car”:1, “van”:0}]

>>> print(nested_lst2)

[{‘camera’: 2, ‘phone’: 1}, {‘car’: 1, ‘van’: 0}]

Accessing nested dictionaries inside a list:

Example 5

>>> nested_lst2 = [{“camera”:2, “phone“:1}, {“car”:1, “van”:0}]

>>> print(nested_lst2[1])

{‘car’: 1, ‘van’: 0}

Accessing inner elements second example:

Example 6

>>> nested_lst2 = [{“camera”:2, “phone“:1}, {“car”:1, “van”:0}]

>>> print(nested_lst2[1][“car”])


Dictionary inside dictionary inside list:

Example 7

>>> nested_lst3 = [{“camera”:{“color”: “black”, “res”: 16}, “phone“:1}, {“car”:{“amount”: 1, “year”: 2017, “color”: “red”}, “van”:0}]

>>> print(nested_lst3[1][“car”][“color”])


Dictionary inside dictionary inside dictionary:

Example 8

>>> nested_lst4 = {“items”: {“camera”:{“color”: “black”, “res”: 16}, “phone“:1}, “assets”: {“car”:{“amount”: 1, “year”: 2017, “color”: “red”}, “van”:0}}

>>> print(nested_lst4[“assets”])

{‘car’: {‘amount’: 1, ‘year’: 2017, ‘color’: ‘red’}, ‘van’: 0}

Accessing nested data summary

Above examples can seem complicated at first glance but really the idea is quite simple. use [] for accessing a list or dictionary item. if you’re accessing a list element type its index number such as: [2], if it’s a dictionary value type its key in quotes such as: [“year”] and feel free to chain these notations as needed if your data is nested such as: data[1][“color”][3][“make”].

Accessing dictionary inside dictionary inside dictionary

Example 9

>>> nested_lst4 = {“items”: {“camera”:{“color”: “black”, “res”: 16}, “phone“:1}, “assets”: {“car”:{“amount”: 1, “year”: 2017, “color”: “red”}, “van”:0}}

>>> print(nested_lst4[“assets”][“car”][“year”])




Example 6

And everything

>>> a 


Advanced Concepts (Optional)

1- Nested Data is a common structure for JSON format which many API applications use.

Here is a typical example demonstrating how your JSON may look after an API access:

Below is nothing but some neat nested data example, but for now you don’t have to worry too much about it as we will cover these topics in greater detail later.


A simple API example showing the nested data output:

>>> req = requests.get(‘http://api.zippopotam.us/us/vt/rutland’)
>>> t = req.json()
>>> data = json.dumps(t, indent=3)

>>> print (data)

“country abbreviation”: “US”,
“places”: [
“place name”: “Rutland”,
“longitude”: “-72.9708”,
“post code”: “05701”,
“latitude”: “43.6141”
“place name”: “Rutland”,
“longitude”: “-72.9906”,
“post code”: “05702”,
“latitude”: “43.4128”
“place name”: “Center Rutland”,
“longitude”: “-73.017”,
“post code”: “05736”,
“latitude”: “43.6023”
“place name”: “West Rutland”,
“longitude”: “-73.0424”,
“post code”: “05777”,
“latitude”: “43.5781”
“country”: “United States”,
“place name”: “Rutland”,
“state”: “Vermont”,
“state abbreviation”: “VT”

Exercise 6

Lesson 7


roger arndt - The-Evening-Visit

Roger Arndt

The Evening Visit

The Evening Visit by Roger Arndt