A dictionary in Python is where we can store key/value pairs.
For example,
student = { "name": "Jane Doe", "birth_year": 1990 }
print(student["name"]) #prints "Jane Doe"
student["name"] = "Joe Smith" #updates the value
print(student["name"]) #prints "Joe Smith"
We can add a new key into the dictionary by doing:
student["country"] = "United States"
We could not only assign a string or a number but also assign a list or even another dictionary! For example,
student["favorite_sports"] = ["basketball, "soccer", "volleyball"]
student["favorite song"] = { "name" : "Beat It", "singer": "Michael Jackson", "year": 1982 }
You can iterate through a dictionary in multiple ways. For example:
for x in student:
print(x) # prints all the keys
for x in student:
print(student[x]) # prints all the values
In this exercise, consider a dictionary such as below:
staff = {
"managers": [ "Michael", "John" ],
"employees": [ "Howard", "Lori", "Elijah" ],
"owner": [ "Bob", "Stuart" ]
}
Given this dictionary, how would you write a function to print each value in this exact format?
managers
- Michael
- John
employees
- Howard
- Lori
- Elijah
owner
- Bob
- Stuart