name = "orange"
dict_fruits = {
name: 7,
"pear": 8,
"apple": 9
}
print(dict_fruits.keys())Review: Dictionaries
Module 2: Data Structures
Review of Session 2.3: Dictionaries
What are the keys of dict_fruits?
What will be the output of this cell?
name = "orange"
dict_fruits = {
name: 7,
"pear": 8,
"apple": 9,
"orange": 10
}
print(dict_fruits[name])What will be the length of dict_fruits?
name = "orange"
dict_fruits = {
name: 7,
"pear": 8,
"apple": 9,
"orange": 7
}
print(dict_fruits)What will be the length of dict_fruits?
dict_fruits = {
"pear": 8,
"apple": 8,
"orange": 8,
}
print(dict_fruits)What will be the length of dict_fruits?
dict_foods = {
"fruits": dict_fruits,
"vegetables": ["carrot", "broccoli"]
}
print(dict_foods)What will be the length of dict_fruits?
dict_fruits = {
"pear": 8,
"apple": 8,
"orange": 8,
}
dict_fruits = {
"fruits": dict_fruits
}
print(dict_fruits)What will be the contents of dict_scores?
ls_students = ["A", "B", "C"]
ls_scores = [9, 10, 8.5]
dict_scores = {}
for idx in range(len(ls_students)):
dict_scores[ls_students[idx]] = ls_scores[idx]
print(ls_students[idx])
print(dict_scores)What will be the length of dc_crops?
num_parsnip = 20
dc_crops = {
"parsnip": num_parsnip,
"potato": 5,
"cauliflower": 10,
"kale": 5,
}
print(len(dc_crops))What will be the length of dc_person?
dc_person = {
"name": "Trisha",
"children": ["Edward", "Alphonse"],
"age": 26,
}
dc_person["gender"] = "female"
print(len(dc_person))What will be the value of the key area?
dc_farm = {
"length": 10,
"width": 5,
"missing": "area"
}
dc_farm["area"] = dc_farm["length"] * dc_farm["width"]
del dc_farm["missing"]
print(dc_farm["area"])What will be the output of the following cell?
nested_dict = {
"mammal": {
"dog": "woof",
"cat": "meow",
},
"fish": {
"tuna": "blub",
"salmon": "blub blub",
},
}
print(nested_dict["mammal"]["dog"])What will be the output of the following cell?
nested_dict = {
"mammal": {
"dog": "woof",
"cat": "meow",
},
"fish": {
"tuna": "blub",
"salmon": "blub blub",
},
}
print(nested_dict["tuna"])The nested_dict contains only two keys: “mammal” and “fish”. Even if nested_dict includes other dictionaries within it, it does not recognize the keys of those inner dictionaries.
As a general rule, structures only consider their outermost index level and do not account for any substructures within them.
What will be the output of the following cell?
nested_dict = {
"mammal": {
"dog": "woof",
"cat": "meow",
},
"fish": {
"tuna": "blub",
"salmon": "blub blub",
},
}
print(nested_dict["fish"]["blub"])Remember, we index dictionaries by their keys. The example above fails because “blub” is a value.
What will be the output of the following cell?
ls_dicts = [
{"name": "Manuel", "country": "Spain"},
{"name": "Louis", "country": "Canada"}
]
print(ls_dicts[-1])What will be the output of the following cell?
ls_dicts = [
{"name": "Manuel", "country": "Spain"},
{"name": "Louis", "country": "Canada"}
]
print(ls_dicts["Manuel"]["country"])ls_dicts is a list, so you need to access its elements using numerical indices. If you want to access the dictionary containing “Manuel”, you should do ls_dicts[0].
What will be the output of the following cell?
ls_dicts = [
{"name": "Manuel", "country": "Spain"},
{"name": "Louis", "country": "Canada"}
]
for d in ls_dicts:
for v in d.values():
print(v)