Lab: Examples of nested dictionaries
# Examples showing different ways to write nested dictionaries
#
# Goal:
# Create a dictionary where keys = state abbreviation
# values = a dictionary of info about that country
# In short: A dictionary of dictionaries
# 3 different approaches:
# Method 1 Using no subscripting
states = {
'nc' : {'capital':'raleigh', 'bird':'cardinal'},
'ca' : {'capital':'sacremento', 'bird':'quail'}
}
# Method 2 Using some subscripting
states = {}
states['nc'] = {'capital':'raleigh', 'bird':'cardinal'}
states['ca'] = {'capital':'sacremento', 'bird':'quail'}
# Method 3 Using all subscripting
states = {}
states['nc'] = {}
states['nc']['capital'] = 'raleigh'
states['nc']['bird'] = 'cardinal'
states['ca'] = {}
states['ca']['capital'] = 'scaremento'
states['ca']['bird'] = 'quail'
# Extracting data from this dictionary
print states['ca'] # {'bird':'quail', 'capital':'sacremento'}
print states['ca']['bird'] # 'quail'
Table of Contents