List and Dictionary purpose

Our society is being built 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 be able collect many instances of that pattern.

To start exploring more deeply into List, Dictionary and Iteration this example will explore constructing a List of people and cars.

# Define an empty 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"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Shane",
    "LastName": "Lopez",
    "DOB": "February 27",
    "Residence": "San Diego",
    "Email": "???@powayusd.com",
    "Owns_Cars": ["2021-Insight"]
})

InfoDb.append({
    "FirstName": "Aidan",
    "LastName": "Lau",
    "DOB": "June 4",
    "Residence": "San Diego",
    "Email": "aidanl17353@powayusd.com",
    "Owns_Cars": ["???"]
})

InfoDb.append({
    "FirstName": "Vince",
    "LastName": "Quach",
    "DOB": "???",
    "Residence": "San Diego",
    "Email": "vinceq11743@powayusd.com",
    "Owns_Cars": ["???"]
})

# Print the data structure
print(InfoDb)
[{'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']}, {'FirstName': 'Sunny', 'LastName': 'Naidu', 'DOB': 'August 2', 'Residence': 'Temecula', 'Email': 'snaidu@powayusd.com', 'Owns_Cars': ['4Runner']}, {'FirstName': 'Shane', 'LastName': 'Lopez', 'DOB': 'February 27', 'Residence': 'San Diego', 'Email': '???@powayusd.com', 'Owns_Cars': ['2021-Insight']}, {'FirstName': 'Aidan', 'LastName': 'Lau', 'DOB': 'June 4', 'Residence': 'San Diego', 'Email': 'aidanl17353@powayusd.com', 'Owns_Cars': ['???']}, {'FirstName': 'Vince', 'LastName': 'Quach', 'DOB': '???', 'Residence': 'San Diego', 'Email': 'vinceq11743@powayusd.com', 'Owns_Cars': ['???']}]

Formatted output of List/Dictionary - for loop

Managing data in Lists and Dictionaries is for the convenience of passing the data across the internet, to applications, or preparing it to be stored into a database. It is a great way to exchange data between programs and programmers. Exchange of data between programs includes the data type the method/function and the format of the data type. These concepts are key to learning how to write functions, receive, and return data. This process is often referred to as an Application Programming Interface (API).

Next, we will take the stored data and output it within our notebook. There are multiple steps to this process…

# This jupyter cell has dependencies on one or more cells above

# 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", "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 algorithm iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record) # call to function

for_loop() # call to function
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

Shane Lopez
	 Residence: San Diego
	 Birth Day: February 27
	 Cars: 2021-Insight

Aidan Lau
	 Residence: San Diego
	 Birth Day: June 4
	 Cars: ???

Vince Quach
	 Residence: San Diego
	 Birth Day: ???
	 Cars: ???

Alternate methods for iteration - while loop

In coding, there are usually many ways to achieve the same result. Defined are functions illustrating using index to reference records in a list, these methods are called a “while” loop and “recursion”.

# This jupyter cell has dependencies on one or more cells above

# 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

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

Shane Lopez
	 Residence: San Diego
	 Birth Day: February 27
	 Cars: 2021-Insight

Aidan Lau
	 Residence: San Diego
	 Birth Day: June 4
	 Cars: ???

Vince Quach
	 Residence: San Diego
	 Birth Day: ???
	 Cars: ???

Calling a function repeatedly - recursion

This final technique achieves looping by calling itself repeatedly.

# This jupyter cell has dependencies on one or more cells above

# 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

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

Shane Lopez
	 Residence: San Diego
	 Birth Day: February 27
	 Cars: 2021-Insight

Aidan Lau
	 Residence: San Diego
	 Birth Day: June 4
	 Cars: ???

Vince Quach
	 Residence: San Diego
	 Birth Day: ???
	 Cars: ???

Calling a function repeatedly - index

for index, person_info in enumerate(InfoDb):
    print(f"Person {index + 1} Info:")
    print("First Name:", person_info["FirstName"])
    print("Last Name:", person_info["LastName"])
    print("DOB:", person_info["DOB"])
    print("Residence:", person_info["Residence"])
    print("Email:", person_info["Email"])
    print("Cars Owned:")
    
    # Iterate through the list of cars owned by the person
    for car in person_info["Owns_Cars"]:
        print("-", car)
    
    print()
Person 1 Info:
First Name: John
Last Name: Mortensen
DOB: October 21
Residence: San Diego
Email: jmortensen@powayusd.com
Cars Owned:
- 2015-Fusion
- 2011-Ranger
- 2003-Excursion
- 1997-F350
- 1969-Cadillac

Person 2 Info:
First Name: Sunny
Last Name: Naidu
DOB: August 2
Residence: Temecula
Email: snaidu@powayusd.com
Cars Owned:
- 4Runner

Person 3 Info:
First Name: Shane
Last Name: Lopez
DOB: February 27
Residence: San Diego
Email: ???@powayusd.com
Cars Owned:
- 2021-Insight

Person 4 Info:
First Name: Aidan
Last Name: Lau
DOB: June 4
Residence: San Diego
Email: aidanl17353@powayusd.com
Cars Owned:
- ???

Person 5 Info:
First Name: Vince
Last Name: Quach
DOB: ???
Residence: San Diego
Email: vinceq11743@powayusd.com
Cars Owned:
- ???

Hacks