toc: true layout: post categories: [week 2]

title: Reversing Lists and Dictionaries

InfoDb = []

# Append to List a Dictionary of key/values
InfoDb.append({
    "FirstName": "Noor",
    "LastName": "Grewal",
    "DOB": "October 27",
    "Residence": "4S Ranch",
    "Email": "noorkg05@gmail.com",
    "Car": "2012-Mazda CX9",
    "Owns Dogs": ["Luna", "Kahlua", "Daisy"],
    "Owns Cats": ["Merlot", "Smeagol"],
    "Favorite Songs": ["Wasting Love - Iron Maiden", "Tear - The Smashing Pumpkins", "Everlong - Foo Fighters"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sanika",
    "LastName": "Shahapurkar",
    "DOB": "September 29",
    "Residence": "Del Sur",
    "Email": "sanika.shahapurkar@gmail.com",
    "Car": "2018-BMW 430i",
    "Owns Dogs": ["Striver"],
    "Owns Cats": ["none:("],
    "Favorite Songs": ["Someone New - Hozier", "Marvelous - Wallows", "Saggitarius Superstar - Coin & Faye Webster"]
})

InfoDb.reverse()

print('Reversed List:', InfoDb)

# Print the data structure
print(InfoDb)
Reversed List: [{'FirstName': 'Sanika', 'LastName': 'Shahapurkar', 'DOB': 'September 29', 'Residence': 'Del Sur', 'Email': 'sanika.shahapurkar@gmail.com', 'Car': '2018-BMW 430i', 'Owns Dogs': ['Striver'], 'Owns Cats': ['none:('], 'Favorite Songs': ['Someone New - Hozier', 'Marvelous - Wallows', 'Saggitarius Superstar - Coin & Faye Webster']}, {'FirstName': 'Noor', 'LastName': 'Grewal', 'DOB': 'October 27', 'Residence': '4S Ranch', 'Email': 'noorkg05@gmail.com', 'Car': '2012-Mazda CX9', 'Owns Dogs': ['Luna', 'Kahlua', 'Daisy'], 'Owns Cats': ['Merlot', 'Smeagol'], 'Favorite Songs': ['Wasting Love - Iron Maiden', 'Tear - The Smashing Pumpkins', 'Everlong - Foo Fighters']}]
[{'FirstName': 'Sanika', 'LastName': 'Shahapurkar', 'DOB': 'September 29', 'Residence': 'Del Sur', 'Email': 'sanika.shahapurkar@gmail.com', 'Car': '2018-BMW 430i', 'Owns Dogs': ['Striver'], 'Owns Cats': ['none:('], 'Favorite Songs': ['Someone New - Hozier', 'Marvelous - Wallows', 'Saggitarius Superstar - Coin & Faye Webster']}, {'FirstName': 'Noor', 'LastName': 'Grewal', 'DOB': 'October 27', 'Residence': '4S Ranch', 'Email': 'noorkg05@gmail.com', 'Car': '2012-Mazda CX9', 'Owns Dogs': ['Luna', 'Kahlua', 'Daisy'], 'Owns Cats': ['Merlot', 'Smeagol'], 'Favorite Songs': ['Wasting Love - Iron Maiden', 'Tear - The Smashing Pumpkins', 'Everlong - Foo Fighters']}]
def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"])  
    print("\t", "Residence:", d_rec["Residence"]) 
    print("\t", "Birthday:", d_rec["DOB"])
    print("\t", "Dogs: ", end="")
    print(", ".join(d_rec["Owns Dogs"])) 
    print("\t", "Cats: ", end="")
    print(", ".join(d_rec["Owns Cats"]))
    print("\t", "Favorite Songs:", d_rec["Favorite Songs"])


# for loop iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)


print('Reversed List:', print_data)

for_loop()
Reversed List: <function print_data at 0x7f2be8159c10>
For loop output

Sanika Shahapurkar
	 Residence: Del Sur
	 Birthday: September 29
	 Dogs: Striver
	 Cats: none:(
	 Favorite Songs: ['Someone New - Hozier', 'Marvelous - Wallows', 'Saggitarius Superstar - Coin & Faye Webster']
Noor Grewal
	 Residence: 4S Ranch
	 Birthday: October 27
	 Dogs: Luna, Kahlua, Daisy
	 Cats: Merlot, Smeagol
	 Favorite Songs: ['Wasting Love - Iron Maiden', 'Tear - The Smashing Pumpkins', 'Everlong - Foo Fighters']