List and Dictionary Basics

Lists and Dictionaries Our society is being build on information. List and Dictionaries are used to collect information. Mostly, when information is collected it is formed into patterns. As that pattern is established you will collect many instances of that pattern.

List is used to collect many Dictionary is used to define data patterns. Iteration is often used to process through lists. To start exploring more deeply into List, Dictionary and Iteration we will explore constructing a List of people and cars.

I created 4 dictionaries within my list.

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": "Nathan",
    "LastName": "Kim",
    "DOB": "December 7",
    "Residence": "San Diego",
    "Email": "nathank51687@stu.powayusd.com",
    "Hobbies": ["Grinding SAT", "Listening to Music", "Watching TV"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Max",
    "LastName": "Wu",
    "DOB": "January 12",
    "Residence": "San Diego",
    "Email": "mwu@powayusd.com",
    "Hobbies": ["Watching TV", "Listening to Music", "Hockey"]
})

# Append to List a 3rd Dictionary of key/values
InfoDb.append({
    "FirstName": "Bob",
    "LastName": "Bobby",
    "DOB": "January 10",
    "Residence": "San Diego",
    "Email": "bbobby@powayusd.com",
    "Hobbies": ["Biking", "Eating", "Walking"]
})

# Append to List a 3rd Dictionary of key/values
InfoDb.append({
    "FirstName": "Robert",
    "LastName": "Lee",
    "DOB": "February 11",
    "Residence": "San Diego",
    "Email": "asmith@powayusd.com",
    "Hobbies": ["Biking", "Eating", "Walking"]
})

How I printed my dictionary content.

#print(InfoDb)

# print function: given a dictionary of InfoDb content
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", "Hobbies: ", end="")  # end="" make sure no return occurs
    print(", ".join(d_rec["Hobbies"]))  # join allows printing a string list with separator
    print()

I tried 3 different methods of printing with for loop, while loop, and for loop with index.

def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        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()

# for loop with index
def for_loop_index():
    print("For loop with index output\n")
    for i in range(0, len(InfoDb), 1):
        record = InfoDb[i]
        print_data(record)
    return

for_loop_index()
For loop output

Nathan Kim
	 Residence: San Diego
	 Birth Day: December 7
	 Hobbies: Grinding SAT, Listening to Music, Watching TV

Max Wu
	 Residence: San Diego
	 Birth Day: January 12
	 Hobbies: Watching TV, Listening to Music, Hockey

Bob Bobby
	 Residence: San Diego
	 Birth Day: January 10
	 Hobbies: Biking, Eating, Walking

Robert Lee
	 Residence: San Diego
	 Birth Day: February 11
	 Hobbies: Biking, Eating, Walking

While loop output

Nathan Kim
	 Residence: San Diego
	 Birth Day: December 7
	 Hobbies: Grinding SAT, Listening to Music, Watching TV

Max Wu
	 Residence: San Diego
	 Birth Day: January 12
	 Hobbies: Watching TV, Listening to Music, Hockey

Bob Bobby
	 Residence: San Diego
	 Birth Day: January 10
	 Hobbies: Biking, Eating, Walking

Robert Lee
	 Residence: San Diego
	 Birth Day: February 11
	 Hobbies: Biking, Eating, Walking

For loop with index output

Nathan Kim
	 Residence: San Diego
	 Birth Day: December 7
	 Hobbies: Grinding SAT, Listening to Music, Watching TV

Max Wu
	 Residence: San Diego
	 Birth Day: January 12
	 Hobbies: Watching TV, Listening to Music, Hockey

Bob Bobby
	 Residence: San Diego
	 Birth Day: January 10
	 Hobbies: Biking, Eating, Walking

Robert Lee
	 Residence: San Diego
	 Birth Day: February 11
	 Hobbies: Biking, Eating, Walking

I also printed in reverse order.

def for_loop_index_reverse():
    print("Reverse order\n")
    for i in range(len(InfoDb)-1, -1, -1):
        record = InfoDb[i]
        print_data(record)
    return

for_loop_index_reverse()
Reverse order

Robert Lee
	 Residence: San Diego
	 Birth Day: February 11
	 Hobbies: Biking, Eating, Walking

Bob Bobby
	 Residence: San Diego
	 Birth Day: January 10
	 Hobbies: Biking, Eating, Walking

Max Wu
	 Residence: San Diego
	 Birth Day: January 12
	 Hobbies: Watching TV, Listening to Music, Skating

Nathan Kim
	 Residence: San Diego
	 Birth Day: December 7
	 Hobbies: Grinding SAT, Listening to Music, Watching TV