lists and dictionaries
hacks for lists/dictionaries week 2
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "Justin",
"LastName": "Nguyen",
"DOB": "July 28 2006",
"Residence": "San Diego",
"Email": "supercatsd@gmail.com",
"favorite_fruit": "apple",
"Owns_Cars": ["none"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "bob",
"LastName": "the",
"DOB": "none",
"Residence": "homeless",
"Email": "no computer",
"favorite_fruit": "banana",
"Owns_Cars": ["expensive car"]
})
# print function: given a dictionary of InfoDb content
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Cars: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print("\t", "Email: ", d_rec["Email"])
print("\t", "favorite fruit: ", d_rec["favorite_fruit"])
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
fruits = {}
for i in range(5):
fruit = input("Enter fruit: ")
color = input("Enter color: ")
fruits[fruit] = color
print("the fruits you typed were: ")
for key in fruits:
print(key)
print("and the colors of those fruits were: ")
for key in fruits:
print(key,":", fruits[key])