names_ages = {"Paul": 32, "Patricia": 44, "Eduardo": 27}
for key in names_ages:
names_ages[key] += 1
names_ages
{'Paul': 33, 'Patricia': 45, 'Eduardo': 28}
You can change dictionary values using keys
It mutates the dictionary, multiplying each value by 0.056 and rounding the result to two decimals
Name file as sales_tax.py
and submit to Gradescope
def sales_tax(dictionary):
for key in dictionary:
dictionary[key] = round(dictionary[key] * 0.056, 2)
return dictionary
def main():
groceries = {"banana": 2.50, "milk": 4.25, "bread": 3.68}
sales_tax(groceries)
assert groceries == {'banana': 0.14, 'milk': 0.24, 'bread': 0.21}
print(groceries)
main()
{'banana': 0.14, 'milk': 0.24, 'bread': 0.21}
key: value
Cannot iterate over a dictionary and change its size:
RuntimeError: dictionary changed size during iteration
key: value
It takes a dictionary as argument, with string keys. It mutates and returns the dictionary argument removing keys that start and end with the same character
Create a list with a total of five 'a'
:
Create a list with a total of three 1
:
It takes a dictionary as argument, with strings as keys and integers as values (assume all values are positive). It mutates and returns the dictionary, replacing each value with a list of the key repeated by the value.
def repetition(dictionary):
for key, value in dictionary.items():
dictionary[key] = [key] * value
return dictionary
def main():
test_dictionary = {"a": 5, "b": 2, "c": 3}
repetition(test_dictionary)
assert test_dictionary == {"a": ["a", "a", "a", "a", "a"],
"b": ["b", "b"], "c": ["c", "c", "c"] }
main()
It has two parameters: a dictionary
that maps one-letter strings to lists of strings, and a string
. It returns a list with strings (names) from the lists in the dictionary that include the second parameter (string
) as a substring (this search is not case sensitive)
Test case:
def get_names(dictionary, string):
new_list = []
for name_list in dictionary.values():
for name in name_list:
if string.lower() in name.lower():
new_list.append(name)
return new_list
def main():
names = { "R": ["Randy", "Roger"],
"S": ["Steve", "Sally"],
"T": ["Tanner"] }
print( get_names(names, "er") ) # ["Roger", "Tanner"]
main()
['Roger', 'Tanner']
You have 10 minutes to complete the quiz.