Sanika's Lists and Dictionaries Reversed
InfoDb = []
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "John",
"LastName": "Mortenson",
"DOB": "October 21",
"Zodiac Sign": "Scorpio",
"Most Famous Songs": ["Code Code Code"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Taylor",
"LastName": "Swift",
"DOB": "December 13",
"Zodiac Sign": "Sagittarius",
"Most Famous Songs": ["Love Story", "Bad Blood", "You Belong With Me"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Harry",
"LastName": "Styles",
"DOB": "February 1",
"Zodiac Sign": "Aquarius",
"Most Famous Songs": ["Sign of the Times", "Golden", "Falling"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Noor",
"LastName": "Grewal",
"DOB": "October 27",
"Zodiac Sign": "Scorpio",
"Most Famous Songs": ["Wasting Love - Iron Maiden", "Tear - Smashing Pumpkins", "Everlong - Foo Fighters"]
})
InfoDb = InfoDb[::-1]
print((InfoDb))
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Zodiac Sign:", d_rec["Zodiac Sign"])
print("\t", "Famous Songs: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Most Famous Songs"]))
# for loop iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
def while_loop():
print("While loop output\n")
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
print("Recursive loop output\n")
recursive_loop(0)