Dictionaries and Bracket Notations in Python
In this activity, you are given two dictionaries. They are actually the same dictionary, except that one contains content, and the other doesn’t. Your job is to convert the dictionary without content into an exact replica of the dictionary that is commented out.
You may not create a new dictionary; instead, use bracket notation to populate the empty san_francisco dictionary. You will know you have completed the challenge when your print statement matches the dictionary that is commented out.
Starter file
# san_francisco = {
# "west_coast": True,
# "has_multiple_bridges": True,
# "known_for_pizza": False,
# "coastal": True,
# "snows": False,
# "very_hot": False,
# "mayor": "London Breed",
# "state": "California",
# "country": "USA",
# "best_food": "burritos",
# "sports_teams": ["Giants", "Warriors", "Forty-Niners"],
# "tallest_building": "SalesForce Building",
# "population": 884363,
# "city_size": "large",
# "median_house_price": 1610000,
# "famous_residents": ["Maya Angelou", "Robert Frost", "Carlos Santana"],
# "homeless_pop": 1150,
# "political_leaning": "Democrat",
# "notable_attractions": ["Alcatraz", "Golden Gate Bridge", "Fisherman's Wharf"],
# "natural_disasters": ["Earthquakes"],
# }
# Re-create the content of the commented out `san_francisco` dictionary by using bracket notation to manually add each of the key-value pairs (including nested objects).
# Print the manually modified `san_francisco` dictionary and confirm the contents match the commented out version.
Instructions
Open the starter file and perform the following:
- Recreate the content of the commented-out
san_franciscodictionary by using bracket notation to manually add each of the key-value pairs (including nested objects). - Print the manually modified
san_franciscodictionary. Confirm that the contents match the commented-out version.
Hint
Using bracket notation with dictionaries to add key-value pairs looks like the following:
san_francisco["west_coast"] = True
Solution
# san_francisco = {
# "west_coast": True,
# "has_multiple_bridges": True,
# "known_for_pizza": False,
# "coastal": True,
# "snows": False,
# "very_hot": False,
# "mayor": "London Breed",
# "state": "California",
# "country": "USA",
# "best_food": "burritos",
# "sports_teams": ["Giants", "Warriors", "Forty-Niners"],
# "tallest_building": "SalesForce Building",
# "population": 884363,
# "city_size": "large",
# "median_house_price": 1610000,
# "famous_residents": ["Maya Angelou", "Robert Frost", "Carlos Santana"],
# "homeless_pop": 1150,
# "political_leaning": "Democrat",
# "notable_attractions": ["Alcatraz", "Golden Gate Bridge", "Fisherman's Wharf"],
# "natural_disasters": ["Earthquakes"],
# }
# Re-create the content of the commented out `san_francisco` dictionary by using bracket notation to manually add each of the key-value pairs (including nested objects).
san_francisco = {}
san_francisco['west_coast'] = True
san_francisco['has_multiple_bridges'] = True
san_francisco['known_for_pizza'] = False
san_francisco['coastal'] = True
san_francisco['snows'] = False
san_francisco['very_hot'] = False
san_francisco['mayor'] = "London Breed"
san_francisco['state'] = "California"
san_francisco['country'] = "USA"
san_francisco['best_food'] = "burritos"
san_francisco['sports_teams'] = ["Giants", "Warriors", "Forty-Niners"]
san_francisco['tallest_building'] = "SalesForce Building"
san_francisco['population'] = 884363
san_francisco['city_size'] = "large"
san_francisco['median_house_price'] = 1610000
san_francisco['famous_residents'] = ["Maya Angelous", "Robert Frost", "Carlos Santana"]
san_francisco['homeless_pop'] = 1150
san_francisco['political_leaning'] = "Democrat"
san_francisco['notable_attractions'] = ["Alcatraz", "Golden Gate Bridge", "Fisherman's Wharf"]
san_francisco['natural_disasters'] = ["Earthquakes"]
# Print the manually modified `san_francisco` dictionary and confirm the contents match the commented out version.
print(san_francisco)