Defining a list called InfoDb

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": "John",
    "LastName": "Mortensen",
    "DOB": "October 21",
    "Residence": "San Diego",
    "Email": "jmortensen@powayusd.com",
    "Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})


# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sunny",
    "LastName": "Naidu",
    "DOB": "August 2",
    "Residence": "Temecula",
    "Email": "snaidu@powayusd.com",
    "Owns_Cars": ["4Runner"]
})


#print(InfoDb[0])

Adding records to the InfoDb

third = {
    "FirstName": "Nanu",
    "LastName": "Shadow",
    "DOB": "August 5",
    "Residence": "San Diego",
    "Email": "san@powayusd.com",
    "Owns_Cars": ["Jeep","4Runner"]
}

# add the dict to List
InfoDb.append(third)


# create dict
fourth = {
    "FirstName": "Lional",
    "LastName": "Messi",
    "DOB": "January 30",
    "Residence": "Barcelona",
    "Email": "LMessi@gmail.com",
    "Owns_Cars": ["Lamborghini","Maserati","Corvette"]
}

# add the dict to List
InfoDb.append(fourth)

InfoDb.append({
    "FirstName": "Dan",
    "LastName": "Mark",
    "DOB": "October 10",
    "Residence": "San Francisco",
    "Email": "DMark@yahoo.com",
    "Owns_Cars": ["2006 toyota sienna", "2011-Hellcat", "2003-Toyota Carolla"]
})
def print_data(d_rec):
    print(d_rec["FirstName"], 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", "Cars: ", end="")  # end="" make sure no return occurs
    print(", ".join(d_rec["Owns_Cars"]))  # join allows printing a string list with separator
    print()

For Loop Iteration

def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
For loop output

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

While Loop Iteration

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

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

Iteration using recursion

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

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

For Loop Iteration With Index

print("for loop with index output\n")
# display indices in the list
for i in range(len(InfoDb)):
    print_data(InfoDb[i])
for loop with index output

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

For Loop Iterate in a Reverse Order

for i in reversed(range(len(InfoDb))) :
    print_data(InfoDb[i])
Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

List Other Methods -- reverse()

print("other methods that can be performed on lists: reverse()")
InfoDb.reverse()
while_loop()
other methods that can be performed on lists: reverse()
While loop output

Dan Mark
	 Residence: San Francisco
	 Birth Day: October 10
	 Cars: 2006 toyota sienna, 2011-Hellcat, 2003-Toyota Carolla

Lional Messi
	 Residence: Barcelona
	 Birth Day: January 30
	 Cars: Lamborghini, Maserati, Corvette

Nanu Shadow
	 Residence: San Diego
	 Birth Day: August 5
	 Cars: Jeep, 4Runner

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Add New Entry to List with User Input

def addNewToInfoDb():
    #print("Enter a key:")
    key = input("Enter a key:")
    #print("Enter value:")
    value = input("Enter value:")
    InfoDb.append({key: value})
    print("You added " + key + " : " + str(value))

addNewToInfoDb()
You added breed : Labarador