Is it possible to statically define a dictionary which contains another dictionary without using another variable?


#1

Question

In this exercise, one of the values in the library dictionary is set from the plays dictionary. Is it possible to statically define a dictionary which contains another dictionary as a value?

Answer

Yes, when you define the dictionary you simply define another dictionary inside the {} brackets. The following code example shows a population dictionary which contains dictionaries for each state that contains cities and population numbers.

population = {"California": {"Los Angeles": 3971883,
                             "San Diego": 1394928,
                             "San Jose": 1026908},
              "Texas": {"Houston": 2296224,
                        "San Antonio": 1469845}
              }