In: Computer Science
Create individual python code cells for each of the three problems below. In each code cell, use any additional variables and comments as needed to program a solution to the corresponding problem. When done, download the ipynb file and submit it to the appropriate dropbox in the course's canvas page
. 1.
a. Cell Number 1 * Recreate the same list of dictionaries you used during assignment 2.09. (Scroll down to see assignment 2.09 instructions) * Create another variable and define it as an empty list. It will store middle names, so name the variable appropriately. * Loop through the list of dictionaries using a `for` loop, and add each middle name to the list created to hold middle names in the previous bullet point.
b. Cell Number 2 * Create a variable and assign it the length of the middle names list created in the previous cell. Use the appropriate instruction to calculate the length instead of just typing the correct number, which is known as hard-coding. (Hint: You don't have to redefine the list in the new cell as long as you ran the previous cell recently) * Use a `while` loop to count up to the length of the middle names list, and print out the index and value of each item in the list using a format string. (Hint: start your counter at `0` and stop iterating before you reach the length by using a `<` operator)
c. Cell Number 3 * Loop through the list of dictionaries defined in the first cell using another `for` loop. (Hint: Again, you do not need to redefine the list of dictionaries from the previous cell as long as you executed it recently) * Nested inside of that loop, loop through the keys in the current dictionary using another `for` loop and the `enumerate()` instruction. * Nested inside of the nested loop, print out the key and value using a format string. (Hint: You'll need to use the key to get the value out of the dictionary) * Also inside of the nested loop, once the index is greater than or equal to 2, break out of the loop.
2.09 assingment instructions for question 1
**Requirements:** * Create a list containing separate dictionaries for at least 3 people (family members, actors, invisible friends, etc.) and assign the list to a variable. Instead of using your actual family members' names, I want you think of different names starting with the same letter as your family member so that I can't be accused of identity theft. * Each person dictionary should have at least 4 key/value pairs: * first name * middle name. * age (rounded to the nearest decade) * your favorite thing about them * Be sure to use the same keys for each person (eg. "first_name" for everyone's first names). * Print out the entire list.
****This requires some effort so please drop a like if you are satisfied with the solution****
I'm providing the solution cell by cell so please refer to the code screenshot for better reference...
Code:
#cell-1
#assignment 2.09
peopleDicts=[{'firstname':'Mark','middlename':'Evans','age':22,'favourite_thing':'behaviour'},{'firstname':'John','middlename':'Wick','age':42,'favourite_thing':'style'},{'firstname':'Tom','middlename':'Holland','age':26,'favourite_thing':'acting'}]
#cell-2
middleNames=[]
for x in peopleDicts:
middleNames.append(x['middlename'])
print(middleNames) #this line is just for reference
#cell-3
for people in peopleDicts:
for details in enumerate(people.keys()):
if(peopleDicts.index(people)>=2):
break
print("%s - %s"%(details[1],people[details[1]]))
Code and output screenshot:
If I run it on normal python compiler this how the code and output looks:
output: