Iterating through dictionaries and Accumulation

Viewed 70

At the moment, I am attempting to accumulate values in dictionaries nested within a dictionary for homework purposes.

a = {
    "Tennis Balls": {
        "User Rating": 15,
        "Available Colors": 5,
        "Location": "Puppy Mart"
    }
}
b = {
    "Squeaky Toy": {
        "User Rating": 48,
        "Available Colors": 3,
        "Location": "Pooch Shop"
    }
}

c = {
    "Stick": {
        "User Rating": 32,
        "Available Colors": 4,
        "Location": "Pooch Shop"
    }
}
d = {
    "Old Shoe": {
        "User Rating": 13,
        "Available Colors": 2,
        "Location": "Puppy Mart"
    }
}
e = {
    "Stuffed Animal": {
        "User Rating": 14,
        "Available Colors": 7,
        "Location": "Dog Express"
    }
}
puppy_toys = {"Toys": [a, b, c, d, e]}

total_user_ratings = 0

for toy in puppy_toys["Toys"]:

    # insert your own code

    individual_user_rating = puppy_toys["Toys"][toy]["User Rating"]
    total_user_ratings += individual_user_rating

print(total_user_ratings)

I have to fill in # your line here with a single line of code that will allow the for loop to iterate through the dictionary puppy_toys and extract the total number of user ratings for toys located at "Pooch Shop", and then store the total in the variable total_user_ratings, but my line of code, shown below, doesn't seem to be working:

if["Location"] = "Pooch Shop":

Thanks for your assistance!

2 Answers

Well, after playing a little, I imagine you want to achieve the sum of User rating by name of Location, so, you have a dictionary with a list actually, correct me if I'm wrong, so you just have to loop it and take the values like this:

a = {
    "Tennis Balls": {
        "User Rating": 15,
        "Available Colors": 5,
        "Location": "Puppy Mart"
    }
}
b = {
    "Squeaky Toy": {
        "User Rating": 48,
        "Available Colors": 3,
        "Location": "Pooch Shop"
    }
}

c = {
    "Stick": {
        "User Rating": 32,
        "Available Colors": 4,
        "Location": "Pooch Shop"
    }
}
d = {
    "Old Shoe": {
        "User Rating": 13,
        "Available Colors": 2,
        "Location": "Puppy Mart"
    }
}
e = {
    "Stuffed Animal": {
        "User Rating": 14,
        "Available Colors": 7,
        "Location": "Dog Express"
    }
}
puppy_toys = {"Toys": [a, b, c, d, e]}

total_user_ratings = 0

for toy in puppy_toys["Toys"]:
    shop = list(toy.values())[0]
    if (shop["Location"] == "Pooch Shop"):
        individual_user_rating = shop["User Rating"]
        total_user_ratings += individual_user_rating

print(total_user_ratings) 

Your line of code has two main problems. Let's go over it:

if["Location"] = "Pooch Shop":

First, you are trying to compare between two expressions. This is done in python with the equality operator which is ==. A single = usually stands for assignment.

Ex:

>>> a = 5
>>> print(a)
5
>>> print(a == 5)  # "is a equal to 5?"
True

The second problem is if["Location"].

In Python the square brackets usually are used as a "getter". A way to acces elements inside an object (like a list or a dictionary).

Ex:

>>> a = ["x", "y", "z"]
>>> print(a[1])
y
>>> b = {"name": "Tomer", "age": 29}
>>> print(b["name"])
Tomer

So you would want the ["Location"] to come after some object.

A hint for what that should be is in the next line of code:

individual_user_rating = puppy_toys["Toys"][toy]["User Rating"]
Related