Lists, Dictionaries, Iteration
Data Abstraction with Python Lists [] and Python Dictionaries {}.
- Making a List with Dictonaries
- Making a Loop
- Reversed Loop
- While Loop
- While Loop Reversed
- Recursion Algorithm
- Reversed Recursion Algorithm
- Nested Loop
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)
# 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()
# 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()
# 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 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()
# 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)
# 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
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()