# Homework 7 Solutions¶

Enter your name and EID here

This homework is due on Mar. 30, 2020 at 12:00pm. Please submit as a PDF file on Canvas. Before submission, please re-run all cells by clicking "Kernel" and selecting "Restart & Run All."

Problem 1 (5 pts): Create a list with the names of your favorite foods. Your list should contain at least 5 different kinds of food. Then, complete the following steps using python. Each step should be completed in a different cell and your results should be printed with print().

1. Sort the list so that the names appear in alphabetical order. Hint: use the function .sort().
2. How many items are in the list? Hint: use the function len().
3. Remove a food from your list and print the length of the list again. Hint: use the function .remove()
4. Add the food "falafel" to your list. Hint: use the function .append()
5. Make a new list in which every food name in your original list is repeated 5 times.
In [1]:
food = ['tacos', 'sushi', 'pizza', 'shawarma', 'pho']
print(food)

['tacos', 'sushi', 'pizza', 'shawarma', 'pho']

In [2]:
food.sort()
print(food)

['pho', 'pizza', 'shawarma', 'sushi', 'tacos']

In [3]:
print(len(food))

5

In [4]:
food.remove('shawarma')
print(len(food))

4

In [5]:
food.append('falafel')
print(food)

['pho', 'pizza', 'sushi', 'tacos', 'falafel']

In [6]:
new_food = food * 5
print(new_food)

['pho', 'pizza', 'sushi', 'tacos', 'falafel', 'pho', 'pizza', 'sushi', 'tacos', 'falafel', 'pho', 'pizza', 'sushi', 'tacos', 'falafel', 'pho', 'pizza', 'sushi', 'tacos', 'falafel', 'pho', 'pizza', 'sushi', 'tacos', 'falafel']


Problem 2 (5 pts): Now imagine that you have spent a week eating only your favorite foods listed in Problem 1. Create a dictionary that contains the names of the food as keys & counts for each time you ate that food as values. For example, if you ate tacos 12 times, the key would be "tacos" and the value would be 12. The counts should just be made-up counts. Then, complete the following steps using python. Each step should be completed in a different cell and your results should be printed with print().

1. Write code that counts how many times you ate. (In other words, add up all of the food counts.) Hint: use a for loop.
2. Add the food "ramen" to your dictionary and give it a count of 7.
3. Print out a list of the keys and a list of the values in your dictionary (i.e., the food names and the counts). Hint: use the functions .keys() and .values().
4. Change the count for one of the foods in your dictionary (i.e., ramen's count from 7 to 10).
5. Remove a key-value pair from your dictionary. Hint: Google "python delete dictionary entry" to find the answer.
In [7]:
food_dict = {'pizza': 5, 'pho': 2, 'tacos': 7, 'sushi': 1, 'falafel': 3}
print(food_dict)

{'pizza': 5, 'pho': 2, 'tacos': 7, 'sushi': 1, 'falafel': 3}

In [8]:
total = 0
for key in food_dict.keys():
total += food_dict[key]
print(total)

18

In [9]:
food_dict['ramen'] = 7
print(food_dict)

{'pizza': 5, 'pho': 2, 'tacos': 7, 'sushi': 1, 'falafel': 3, 'ramen': 7}

In [10]:
print(food_dict.keys())
print(food_dict.values())

dict_keys(['pizza', 'pho', 'tacos', 'sushi', 'falafel', 'ramen'])
dict_values([5, 2, 7, 1, 3, 7])

In [11]:
food_dict['ramen'] = 10
print(food_dict)

{'pizza': 5, 'pho': 2, 'tacos': 7, 'sushi': 1, 'falafel': 3, 'ramen': 10}

In [12]:
del food_dict['pho']
print(food_dict)

{'pizza': 5, 'tacos': 7, 'sushi': 1, 'falafel': 3, 'ramen': 10}