WorksheetsSection 3D: Collect and process data using dictionaries
Total questions: 15
Worksheet time: 8mins
Which of the following is the correct way to create an empty dictionary in Python?
`my_dict = []`
`my_dict = {}`
`my_dict = ()`
`my_dict = set()`
What will be the output of the following code? ```python my_dict = {'a': 1, 'b': 2} print(my_dict['b']) ```
`'b'`
`1`
`2`
`KeyError`
Which method would you use to get a list of all the keys in a dictionary?
`values()`
`items()`
`keys()`
`get()`
How do you add a new key-value pair to an existing dictionary called `d` with key `'x'` and value `10`?
`d.add('x', 10)`
`d['x'] = 10`
`d.append('x', 10)`
`d.insert('x', 10)`
Which of the following statements will remove the key `'age'` from the dictionary `person`?
`person.remove('age')`
`del person['age']`
`person.popitem('age')`
`person.clear('age')`
Given the dictionary `d = {'a': 1, 'b': 2, 'c': 3}`, which code will check if the key `'b'` exists in `d`?
`'b' in d`
`d.has_key('b')`
`'b' == d`
`d['b'] == None`
Which method returns a view object that displays a list of a dictionary's key-value tuple pairs?
`keys()`
`values()`
`items()`
`pairs()`
Suppose you have the dictionary `d = {'x': 5, 'y': 10}`. Which code will print all the values in the dictionary?
`print(d.keys())`
`print(d.items())`
`print(d.values())`
`print(d.all())`
Given `d = {'a': 1, 'b': 2, 'c': 3}`, which code will iterate through all the keys and print them?
`for key in d: print(key)`
`for key in d.keys(): print(key)`
Both A and B
`for key in d.values(): print(key)`
If you want to remove a key-value pair from a dictionary and get its value at the same time, which method should you use?
`remove()`
`pop()`
`del`
`clear()`
You have a dictionary `grades = {'Alice': 85, 'Bob': 90, 'Charlie': 78}`. Write a code snippet to increase every student's grade by 5 points.
```python for key in grades: grades[key] += 5 ```
```python for value in grades: value += 5 ```
```python grades.values() += 5 ```
```python for key, value in grades.items(): key += 5 ```
Given `d = {'a': 1, 'b': 2, 'c': 3}`, which code will print both keys and values?
```python for key in d: print(key, d[key]) ```
```python for key, value in d.items(): print(key, value) ```
Both A and B
```python for value in d.values(): print(value) ```
Suppose you want to count the number of times each word appears in a list: `words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']`. Which of the following code snippets will correctly build a dictionary with word counts?
```python counts = {} for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 ```
```python counts = {} for word in words: counts[word] = 1 ```
```python counts = {} for word in words: counts[word] += 1 ```
```python counts = {} for word in words: counts.add(word) ```
You have a dictionary `inventory = {'apples': 10, 'bananas': 5, 'oranges': 8}`. Write a code snippet to remove all items with a value less than 8.
```python for key in inventory: if inventory[key] < 8: del inventory[key] ```
```python for key in list(inventory.keys()): if inventory[key] < 8: del inventory[key] ```
```python for key, value in inventory.items(): if value < 8: inventory.pop(key) ```
```python inventory.clear() ```
Given a dictionary `d = {'a': 1, 'b': 2, 'c': 3}` and a list `keys_to_remove = ['b', 'c', 'd']`, which code will remove only the keys that exist in `d`?
```python for key in keys_to_remove: del d[key] ```
```python for key in keys_to_remove: if key in d: del d[key] ```
```python for key in d: if key in keys_to_remove: del d[key] ```
```python d.clear() ```
