Lists, Dictionaries, Iteration

  • Add a couple of records to the InfoDb
InfoDb = []

# 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": "johnmortensen.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 Dictionary of key/values related to a person and cars
InfoDb.append({
    "FirstName": "Theo",
    "LastName": "Huntalas",
    "DOB": "January 31",
    "Residence": "San Diego",
    "Email": "theo.h131@gmail.com",
    "Owns_Cars": ["2020-Shelby"]
})

InfoDb.append({
    "FirstName": "Kush",
    "LastName": "Sironi",
    "DOB": "March 9",
    "Residence": "San Diego",
    "Email": "kushsironi@gmail.com",
    "Owns_Cars": ["2019-Bently", "2016-Bugatti"]
})

InfoDb.append({
    "FirstName": "Bob",
    "LastName": "Farmer",
    "DOB": "December 17",
    "Residence": "Chula Vista",
    "Email": "bobthefarmer@gmail.com",
    "Owns_Cars": ["2021-Tractor"]
})

# Print the data structure
print(InfoDb)

# given and index this will print 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()
[{'FirstName': 'John', 'LastName': 'Mortensen', 'DOB': 'October 21', 'Residence': 'San Diego', 'Email': 'johnmortensen.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': 'Theo', 'LastName': 'Huntalas', 'DOB': 'January 31', 'Residence': 'San Diego', 'Email': 'theo.h131@gmail.com', 'Owns_Cars': ['2020-Shelby']}, {'FirstName': 'Kush', 'LastName': 'Sironi', 'DOB': 'March 9', 'Residence': 'San Diego', 'Email': 'kushsironi@gmail.com', 'Owns_Cars': ['2019-Bently', '2016-Bugatti']}, {'FirstName': 'Bob', 'LastName': 'Farmer', 'DOB': 'December 17', 'Residence': 'Chula Vista', 'Email': 'bobthefarmer@gmail.com', 'Owns_Cars': ['2021-Tractor']}]
  • Try to do a for loop with an index
InfoDb = []

# 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": "johnmortensen.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 Dictionary of key/values related to a person and cars
InfoDb.append({
    "FirstName": "Theo",
    "LastName": "Huntalas",
    "DOB": "January 31",
    "Residence": "San Diego",
    "Email": "theo.h131@gmail.com",
    "Owns_Cars": ["2020-Shelby"]
})

InfoDb.append({
    "FirstName": "Kush",
    "LastName": "Sironi",
    "DOB": "March 9",
    "Residence": "San Diego",
    "Email": "kushsironi@gmail.com",
    "Owns_Cars": ["2019-Bently", "2016-Bugatti"]
})

InfoDb.append({
    "FirstName": "Bob",
    "LastName": "Farmer",
    "DOB": "December 17",
    "Residence": "Chula Vista",
    "Email": "bobthefarmer@gmail.com",
    "Owns_Cars": ["2021-Tractor"]
})

# Print the data structure
print(InfoDb)

# given and index this will print 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 iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
[{'FirstName': 'John', 'LastName': 'Mortensen', 'DOB': 'October 21', 'Residence': 'San Diego', 'Email': 'johnmortensen.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': 'Theo', 'LastName': 'Huntalas', 'DOB': 'January 31', 'Residence': 'San Diego', 'Email': 'theo.h131@gmail.com', 'Owns_Cars': ['2020-Shelby']}, {'FirstName': 'Kush', 'LastName': 'Sironi', 'DOB': 'March 9', 'Residence': 'San Diego', 'Email': 'kushsironi@gmail.com', 'Owns_Cars': ['2019-Bently', '2016-Bugatti']}, {'FirstName': 'Bob', 'LastName': 'Farmer', 'DOB': 'December 17', 'Residence': 'Chula Vista', 'Email': 'bobthefarmer@gmail.com', 'Owns_Cars': ['2021-Tractor']}]
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

Theo Huntalas
	 Residence: San Diego
	 Birth Day: January 31
	 Cars: 2020-Shelby

Kush Sironi
	 Residence: San Diego
	 Birth Day: March 9
	 Cars: 2019-Bently, 2016-Bugatti

Bob Farmer
	 Residence: Chula Vista
	 Birth Day: December 17
	 Cars: 2021-Tractor

  • Pair Share code somethings creative or unique, with loops and data. Hints... Would it be possible to output data in a reverse order? Are there other methods that can be performed on lists? Could you create new or add to dictionary data set? Could you do it with input? Make a quiz that stores in a List of Dictionaries.
InfoDb = []

# 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": "johnmortensen.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 Dictionary of key/values related to a person and cars
InfoDb.append({
    "FirstName": "Theo",
    "LastName": "Huntalas",
    "DOB": "January 31",
    "Residence": "San Diego",
    "Email": "theo.h131@gmail.com",
    "Owns_Cars": ["2020-Shelby"]
})

InfoDb.append({
    "FirstName": "Kush",
    "LastName": "Sironi",
    "DOB": "March 9",
    "Residence": "San Diego",
    "Email": "kushsironi@gmail.com",
    "Owns_Cars": ["2019-Bently", "2016-Bugatti"]
})

InfoDb.append({
    "FirstName": "Bob",
    "LastName": "Farmer",
    "DOB": "December 17",
    "Residence": "Chula Vista",
    "Email": "bobthefarmer@gmail.com",
    "Owns_Cars": ["2021-Tractor"]
})

# Reversed List
InfoDbReverse = []


InfoDbReverse.append({
    "FirstName": "Bob",
    "LastName": "Farmer",
    "DOB": "December 17",
    "Residence": "Chula Vista",
    "Email": "bobthefarmer@gmail.com",
    "Owns_Cars": ["2021-Tractor"]
})


InfoDbReverse.append({
    "FirstName": "Kush",
    "LastName": "Sironi",
    "DOB": "March 9",
    "Residence": "San Diego",
    "Email": "kushsironi@gmail.com",
    "Owns_Cars": ["2019-Bently", "2016-Bugatti"]
})

InfoDbReverse.append({
    "FirstName": "Theo",
    "LastName": "Huntalas",
    "DOB": "January 31",
    "Residence": "San Diego",
    "Email": "theo.h131@gmail.com",
    "Owns_Cars": ["2020-Shelby"]
})

InfoDbReverse.append({
    "FirstName": "Sunny",
    "LastName": "Naidu",
    "DOB": "August 2",
    "Residence": "Temecula",
    "Email": "snaidu@powayusd.com",
    "Owns_Cars": ["4Runner"]
})

InfoDbReverse.append({
    "FirstName": "John",
    "LastName": "Mortensen",
    "DOB": "October 21",
    "Residence": "San Diego",
    "Email": "johnmortensen.com",
    "Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})


# for loop iterates on length of InfoDb
def for_loop():
    for record in InfoDb:
        print_data(record)

for_loop()

print("------------------Reversed List------------------")

# for loop iterates on length of InfoDb
def for_loop_reverse():
    for record in InfoDbReverse:
        print_data(record)

for_loop_reverse()
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

Theo Huntalas
	 Residence: San Diego
	 Birth Day: January 31
	 Cars: 2020-Shelby

Kush Sironi
	 Residence: San Diego
	 Birth Day: March 9
	 Cars: 2019-Bently, 2016-Bugatti

Bob Farmer
	 Residence: Chula Vista
	 Birth Day: December 17
	 Cars: 2021-Tractor

------------------Reversed List------------------
Bob Farmer
	 Residence: Chula Vista
	 Birth Day: December 17
	 Cars: 2021-Tractor

Kush Sironi
	 Residence: San Diego
	 Birth Day: March 9
	 Cars: 2019-Bently, 2016-Bugatti

Theo Huntalas
	 Residence: San Diego
	 Birth Day: January 31
	 Cars: 2020-Shelby

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