Making a List with Dictonaries

  • I defined some keys and values on information about me and my partner
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": "Alexa",
    "MiddleName": "Rose",
    "LastName": "Carlson",
    "DOB": "May 16",
    "Residence": "San Diego",
    "Email": "alexarosecarlson.com",
    "Owns_Cars": ["Tesla-Model-3", "Mercedes-GL450", "Ford-Expedition"],
    "Phone": "8582695083",
    "Pets": ["Dog"],
    "Favorite_Colors": ["Blue", "Purple"],
    "Phone_Provider": "Verizon"   
})


print(InfoDb)

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Ava",
    "MiddleName": "Brynn",
    "LastName": "Carlson",
    "DOB": "May 16",
    "Residence": "San Diego",
    "Email": "avabrynncheer@gmail.com",
    "Owns_Cars": ["Tesla-Model-3", "Mercedes-GL450", "Ford-Expedition"],
    "Phone": "8582695084",
    "Pets": ["Dog"],
    "Favorite_Colors": ["Pink", "Blue"],
    "Phone_Provider": "Verizon"
})

print()
print()

# Print the data structure
print(InfoDb)
[{'FirstName': 'Alexa', 'MiddleName': 'Rose', 'LastName': 'Carlson', 'DOB': 'May 16', 'Residence': 'San Diego', 'Email': 'alexarosecarlson.com', 'Owns_Cars': ['Tesla-Model-3', 'Mercedes-GL450', 'Ford-Expedition'], 'Phone': '8582695083', 'Pets': ['Dog'], 'Favorite_Colors': ['Blue', 'Purple'], 'Phone_Provider': 'Verizon'}]


[{'FirstName': 'Alexa', 'MiddleName': 'Rose', 'LastName': 'Carlson', 'DOB': 'May 16', 'Residence': 'San Diego', 'Email': 'alexarosecarlson.com', 'Owns_Cars': ['Tesla-Model-3', 'Mercedes-GL450', 'Ford-Expedition'], 'Phone': '8582695083', 'Pets': ['Dog'], 'Favorite_Colors': ['Blue', 'Purple'], 'Phone_Provider': 'Verizon'}, {'FirstName': 'Ava', 'MiddleName': 'Brynn', 'LastName': 'Carlson', 'DOB': 'May 16', 'Residence': 'San Diego', 'Email': 'avabrynncheer@gmail.com', 'Owns_Cars': ['Tesla-Model-3', 'Mercedes-GL450', 'Ford-Expedition'], 'Phone': '8582695084', 'Pets': ['Dog'], 'Favorite_Colors': ['Pink', 'Blue'], 'Phone_Provider': 'Verizon'}]

Making a Loop

  • this loop prints the records I defined and listed in the last cell
# print function: given a dictionary of InfoDb content
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["MiddleName"], 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", "Phone:", d_rec["Phone"])
    print("\t", "Pets: ", end="")  
    print(", ".join(d_rec["Pets"]))  
    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", "Favorite Colors: ", end="")  
    print(", ".join(d_rec["Favorite_Colors"])) 
    print("\t", "Phone Provider:", d_rec["Phone_Provider"])
    print()


# 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()
For loop output

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

Reversed Loop

  • this is the last loop, but reversed
# print function: given a dictionary of InfoDb content
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["MiddleName"], 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", "Phone:", d_rec["Phone"])
    print("\t", "Pets: ", end="")  
    print(", ".join(d_rec["Pets"]))  
    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", "Favorite Colors: ", end="")  
    print(", ".join(d_rec["Favorite_Colors"])) 
    print("\t", "Phone Provider:", d_rec["Phone_Provider"])
    print()


# for loop algorithm iterates on length of InfoDb
def for_loop():
    print("For loop output reversed\n")
    for record in reversed(InfoDb): # this reverses the data in the InfoDB list
        print_data(record)

for_loop()
For loop output reversed

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

While Loop

  • this kind of loop defines a variable and uses that to print the next record
# while loop algorithm contains an initial n and an index incrementing statement (n += 1)
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()
While loop output

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

While Loop Reversed

  • this is the while loop reversed
# while loop algorithm contains an initial n and an index incrementing statement (n += 1)
def while_loop():
    print("While loop output reversed\n")
    i = len(InfoDb)
    while i > 0: # i has to be greater than 0 now that its is reversed (starting at 2 and can't go below 0)
        record = InfoDb[i-1] # lists begin at 0, however, length begins at 1 (must -1 to have same record)
        print_data(record)
        i -= 1 # the - allows to go in reverse order
    return

while_loop()
While loop output reversed

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

Recursion Algorithm

  • A recursion algorithm calls for itself in it's definition
# recursion algorithm loops incrementing on each call (n + 1) until exit condition is met
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)
Recursive loop output

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

Reversed Recursion Algorithm

  • this is the recursion algorithm reversed
# recursion algorithm loops incrementing on each call (n + 1) until exit condition is met
def recursive_loop(i):
    if i >= 0: # must be greater than or equal to 0 since the first record in length is 0 in the list
        record = InfoDb[i]
        print_data(record)
        recursive_loop(i - 1) # -1 makes it reversed
    return
    
print("Recursive loop output reversed\n")
recursive_loop(len(InfoDb)-1) # once again the length is one more than the record in the list so there is -1
Recursive loop output reversed

Ava Brynn Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695084
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Pink, Blue
	 Phone Provider: Verizon

Alexa Rose Carlson
	 Residence: San Diego
	 Birth Day: May 16
	 Phone: 8582695083
	 Pets: Dog
	 Cars: Tesla-Model-3, Mercedes-GL450, Ford-Expedition
	 Favorite Colors: Blue, Purple
	 Phone Provider: Verizon

Nested Loop

  • In this nested loop, there is a for loop inside of another for loop. It will run through the inner loop before the outer loop moves onto the next input. In this case, it will multiply the inner loop number by the outer loop number.
from re import A
for i in range(1, 11):                           
    for a in range(1, 11): # this is the nested loop that will iterate from 1-10
        print(i * a, end=' ') # print the multiplication products
    print()
1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100