Making a List with Dictionaries

  • This is my list of keys and values describing my sister and me. I define these keys and values for future use in my loops.
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)
[{'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']}, {'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']}]

Making a Loop

  • In this loop, I am having the function print line by line each key and value.
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()
For loop output

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Reversing my Loop

  • I reveresed my loop here by adding "reversed" in front of my list when prompted to print.
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()
For loop output reversed

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Making a While Loop

  • In this loop, I am defining a variable and while the variable is less than the length of my list, the funtion will continue to run.
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()
While loop output

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Reversing my While Loop

  • I reversed this loop by switching my variable for i, and making the function subtract one from i every loop to condown instead of up.
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()
While loop output

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Making a Recursion loop

  • In this loop, I am plugging in a term (the number record) into my function so the function can print its data. The term plugged in will increase every time the function loops.
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)
Recursive loop output

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Reversing my Recursive Loop

  • Here I reversed my loop by starting at the length of my list and subtracting one from i until it is at zero.
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
Recursive loop output reversed

Alexa Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: alexarosecarlson@icould.com
	 Favorite Foods: pineapple, pasta, ice cream
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava

Ava Carlson
	 Birth Day: May 16
	 Residence: San Diego
	 Email: avabrynncheer@gmail.com
	 Favorite Foods: mac and cheese, strawberries, chocolate
	 Family Names: Neil, Leslie, Dylan, Grant, Alexa, Ava