Introduction

Welcome to our exploration of Compound Data Structures in Python. Having navigated through Dictionaries, Sets, and Tuples, we'll delve into nested dictionaries and lists. These structures enable us to handle complex and hierarchical data, which is typical in real-world scenarios. This lesson will guide you through a recap of the basics, the creation and modification of nested dictionaries and lists, as well as common error handling.

Recap: Dictionaries, Lists, and Understanding Nested Structures

As a quick recap, Lists are mutable, ordered collections, while Dictionaries are collections of key-value pairs. These structures can be nested. Here's a simple example of a school directory:

# Dicitionary with grades as keys and lists of students as values
school_directory = {
    'Grade1': ['Amy', 'Bobby', 'Charlie'],
    'Grade2': ['David', 'Eve', 'Frank'],
    'Grade3': ['George', 'Hannah', 'Ivy']
}

# Prints the Grade1 list in the dictionary
print(school_directory['Grade1']) # Output: ['Amy', 'Bobby', 'Charlie']
Creating Nested Dictionaries and Lists

Just like their non-nested versions, creating nested structures is straightforward.

Nested Dictionary:

# Dictionary within a dictionary
nested_dict = {
    'fruit':  {
        'apple': 'red',  # key-value pair within the 'fruit' dictionary
        'banana': 'yellow'  # another key-value pair within the 'fruit' dictionary
    },
    'vegetable': {
        'carrot': 'orange',
        'spinach': 'green'
    }
}

# Prints the nested dictionary 
print(nested_dict)

Nested List:

# Lists within a list
nested_list = [
    [1, 2, 3],  # inner list within the outer list
    [4, 5, 6],  # another inner list within the outer list
    [7, 8, 9]   # third inner list within the outer list
]

# Prints the nested list
print(nested_list)

Nested Dictionary and Lists:

# Lists within a dictionary
list_dict = {
    'numbers': [1, 2, 3],  # keys associated with lists
    'letters': ['a', 'b', 'c']
}

# Prints the dictionary of lists
print(list_dict)
Accessing Values in Nested Structures

The retrieval of values from nested dictionaries or lists follows rules similar to those for their non-nested counterparts.

From Nested Dictionary:

# Dictionary within a dictionary
nested_dict = {
    'fruit':  {
        'apple': 'red',  # key-value pair within the 'fruit' dictionary
        'banana': 'yellow'  # another key-value pair within the 'fruit' dictionary
    },
    'vegetable': {
        'carrot': 'orange',
        'spinach': 'green'
    }
}

# Accessing apple's color from nested dictionary
print(nested_dict['fruit']['apple']) # Output: 'red'

From Nested List:

# Lists within a list
nested_list = [
    [1, 2, 3],  # inner list within the outer list
    [4, 5, 6],  # another inner list within the outer list
    [7, 8, 9],  # third inner list within the outer list
]

# Accessing the 3rd value from the 2nd list in nested list
print(nested_list[1][2]) # Output: 6

From Both:

# Lists within a dictionary
list_dict = {
    'numbers': [1, 2, 3],  # keys associated with lists
    'letters': ['a', 'b', 'c']
}

# Accessing the second letter in the 'letters' list in list_dict
print(list_dict['letters'][1]) # Output: 'b'
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal