Making Lists, Dictionaries, Iteration
Trying out Python Lists [] and Python Dictionaries {}.
- Making a List with Dictionaries
- Making a Loop
- Reversing my Loop
- Making a While Loop
- Reversing my While Loop
- Making a Recursion loop
- Reversing my Recursive Loop
avalist = []
# Append to list a dictionary of keys and values about Ava
avalist.append({
"FirstName": "Ava",
"LastName": "Carlson",
"DOB": "May 16",
"Residence": "San Diego",
"Email": "avabrynncheer@gmail.com",
"FavFoods": ["mac and cheese", "strawberries", "chocolate"],
"FamilyNames": ["Neil", "Leslie", "Dylan", "Grant", "Alexa", "Ava"]
})
# Append to list a 2nd Dictionary of key/values about Alexa
avalist.append({
"FirstName": "Alexa",
"LastName": "Carlson",
"DOB": "May 16",
"Residence": "San Diego",
"Email": "alexarosecarlson@icould.com",
"FavFoods": ["pineapple", "pasta", "ice cream"],
"FamilyNames": ["Neil", "Leslie", "Dylan", "Grant", "Alexa", "Ava"]
})
# Print avalist
print(avalist)
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "Birth Day:", d_rec["DOB"]) # adding an indent
print("\t", "Residence:", d_rec["Residence"])
print("\t", "Email:", d_rec["Email"])
print("\t", "Favorite Foods: ", end="")
print(", ".join(d_rec["FavFoods"])) # join prints values together
print("\t", "Family Names: ", end="" )
print(", ".join(d_rec["FamilyNames"]))
print()
def for_loop():
print("For loop output\n")
for record in(avalist):
print_data(record)
for_loop()
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "Birth Day:", d_rec["DOB"]) # adding an indent
print("\t", "Residence:", d_rec["Residence"])
print("\t", "Email:", d_rec["Email"])
print("\t", "Favorite Foods: ", end="")
print(", ".join(d_rec["FavFoods"])) # join prints values together
print("\t", "Family Names: ", end="" )
print(", ".join(d_rec["FamilyNames"]))
print()
def for_loop():
print("For loop output reversed\n")
for record in reversed(avalist): #adding reversed reverses the records listed
print_data(record)
for_loop()
def while_loop():
print("While loop output\n")
i = 0
while i < len(avalist): #len= the length of my list
record = avalist[i]
print_data(record)
i += 1
return
while_loop()
def while_loop():
print("While loop output\n")
i = len(avalist) #the function starts at the length of my list
while i > 0:
record = avalist[i-1]
print_data(record)
i -= 1 #here the loop is counting down
return
while_loop()
def recursive_loop(i):
if i < len(avalist):
record = avalist[i]
print_data(record)
recursive_loop(i + 1) #the function is adding one to i every time it loops
return
print("Recursive loop output\n")
recursive_loop(0)
def recursive_loop(i):
if i >= 0:
record = avalist[i]
print_data(record)
recursive_loop(i - 1) #here is my function subracting one everytime
return
print("Recursive loop output reversed\n")
recursive_loop(len(avalist)-1) #here is my starting point