Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Last active September 14, 2021 15:27
Show Gist options
  • Save ryanorsinger/fce8154028a924c1073eac24c7c3f409 to your computer and use it in GitHub Desktop.
Save ryanorsinger/fce8154028a924c1073eac24c7c3f409 to your computer and use it in GitHub Desktop.
Working with Lists of Dictionaries
# Consider the following list containing 2 dictionaries.
# Remember, lists are ordered lists with a numeric index and dictionaries are labeled lists where each key denotes its value.
# In a way, think of each dictionary's key as a variable that holds the value on the right of the colon symbol.
fruits = [
{
"item": "apple",
"quantity": 5,
"price": 0.95
},
{
"item": "orange",
"quantity": 7,
"price": 0.99
}
]
# How do we access the first dictionary in the list?
fruits[0] # is the first dictionary in the list
# How would we get the number of apples?
fruits[0]["quantity"]
# How would we determine the total spent on apples where we multiply price * quantity?
total_spent_on_apples = fruits[0]["quantity"] * fruits[0]["price"]
print("The total spent on apples is", total_spent_on_apples)
# What's one way to determine the total quantity of all items in the fruist list of dictionaries?
total_quantity = fruits[0]["quantity"] + fruits[1]["quantity"]
# What about if we have a longer list than only two? What if we need this to be programmatic? Use a loop!
total_quantity = 0
for fruit in fruits:
total_quantity = fruit["quantity"]
print("The total number of both apples and oranges is", total_quantity)
# What's one way to determine the total spent on both apples and oranges? (this is the "brute force" way)
spent_on_apples = fruits[0]["quantity"] * fruits[0]["price"]
spent_on_oranges = fruits[1]["quantity"] * fruits[1]["price"]
total_spent = spent_on_apples + spent_on_oranges
print("The total spend on all apples and all oranges is", total_spent)
# What's a programmatic way to determine the total spend? Use a loop! Then access each dictionary's values inside the loop
total_spent = 0 # We often need to start a variable at zero to give it a starting state. Then, future operations change its value.
for fruit in fruits:
total_spent += fruit["quantity"] * fruit["price"] # go through the entire list and multiply each fruit's price by that fruit's quantity
print("The total spend on all apples and all oranges is", total_spent)
# How would we determine the average price spent per item?
total_spent = 0
quantity_of_items = 0
for fruit in fruits:
total_spent += fruit["quantity"] * fruit["price"]
quantity_of_items += fruit["quantity"]
average_per_item = total_spent / quantity_of_items
print("After purchasing a combination of", quantity_of_items, "apples and oranges, the average cost per item is", average_per_item)
@teceno
Copy link

teceno commented Sep 14, 2021

I believe line 34 should be:
total_quantity += fruit["quantity"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment