Even more Dimensions...

After reading the title to this page you might be a little afraid of what's to come. Don't be! Just how we can have lists inside of lists, we can also have dictionaries inside of dictionaries. The important thing to note is that a key in a dictionary cannot be a dictionary itself (only immutable objects can be dictionary keys. so things like strings and numbers are okay as keys, but lists and dictionaries are not). However, this does not stop us from having the values of a dictionary be dictionaries. Shown below is an example of how you can make a 2D dictionary:


>>> alonzo = {"age": 10, "height": 42, "weight": 175, "instrument ": "fiddle" }
>>> turing = {"age": 41, "height": 70 "weight": 160, "instrument": "theremin"}
>>> bertha = {"age": 32, "height": 97, "weight": 587, "instrument": "cello"}
>>> tinkerB = {"age":100, "height": 4, "weight": 0.5, "instrument": "cello"}
>>> banditos = {"Alonzo": alonzo, "Turing": turing, "Bertha": bertha, "TinkerB": tinkerB}
        

To return the sub-dictionary representing Alonzo's attributes we do:


>>> banditos["Alonzo"]
{"age": 10, "height": 42, "weight": 175, "instrument ": "fiddle" }
        

To return a specific trait of Alonzo's we do:


>> banditos["Alonzo"]["age"]
10
        

We can also assign values to the sub-dictionary in a similar way:


>>> banditos["Alonzo"]["age"] = 15
>>> banditos["Alonzo"]["age"]
15
        

Exercise 6: Find Players

Given a dictionary of people, like the one above, returns a new dictionary that contains only the people who play a certain instrument. You can test this exercise with function number 6.


>>> find_players(banditos, "cello")
{"Bertha": {"age": 32, "height": 97, "weight": 587, "instrument": "cello"}, "TinkerB": {"age":100, "height": 4, "weight": 0.5, "instrument": "cello"}}
        

Challenge Exercise 3: Most Popular Friend

It's time for a cringeworthy highschool favorite. Your class decides to rank everyone in the class in order of preference. As class nerd, you've been assigned the task of finding the most popular student. Given a dictionary of each student and a corresponding dictionary of their preferences, write a function that returns a student with the lowest average score. Test this function with function number C3.


>>> alice_ratings = {"alonzo": 1, "bob": 3, "turing" : 2}
>>> bob_ratings = {"alice": 1, "alonzo": 2, "turing": 3}
>>> alonzo_ratings = {"alice": 3, "bob": 2, "turing": 1}
>>> turing_ratings = {"alice": 2, "alonzo": 1 "bob": 3}
>>> friends = {"alice": alice_ratings, "bob": bob_ratings, "alonzo": alonzo_ratings, "turing": turing_ratings}
>>> most_popular(friends)
"alonzo"
        

Well you've survived another Python lab, congratulations! To run all of the tests enter the following command:


python3 PythonLab2.py All