Practice Problems #12:Laptop

I’m confused about the else portion of this code.

price_to_name = {}

for row in catalog_data[1:]:
    price = int(row[2])
    names = row[1]
    if price in price_to_name:
        price_to_name[price].append(names)  
    else:
        price_to_name[price] = [names]

Why is names in ?
Why can’t we just use:

 price_to_name[price] = names 

and add the two items as we did in the earlier problems?

1 Like

Hi @AlJens,

I’ll start with else first, then I’ll continue with if.

  1. If the price is not in the dictionary (meaning that else-statement works in this case), then we add that price as a dictionary key and the name (in your code it’s called names) of the laptop with that price as a value of that key. However, we add the dictionary value not as a string names, but as a list [names], consisting, for now, of only one item.

  2. If the price is already in the dictionary (meaning that if-statement works in this case), then the name of the laptop with that price (names) is appended to the list of the laptop names with that price, already existing in the dictionary (and this list of the laptop names actually represents the value of the dictionary, where the key is price).

By the way, I would suggest you to rename your variable names into name, to avoid further confusion. Because at each iteration of your for-loop, you are actually dealing with only 1 laptop name, hence using name looks more logical.

5 Likes

Hi @Elena_Kosourova ,

Just following up your explanation, I still don’t get why the solution use if/else to create the dictionary. Why can’t we directly create use below code?
price_to_name[price] = name

We used the above code in the previous problems (problems 5/6)without if/else to create the dictionary.

Thank you

1 Like

Hi @czhao001,

No, here it’s a different case. We have to create a dictionary where the keys are the unique values of price and the dictionary values are the lists of items with the same price. For some unique values of price, probably, such a list can consist of only one item, but for the others there will be several items with the same price.

If we create a dictionary directly, like in your piece of code, we will re-write the item with the last one, instead of creating a list of items.

Hi @Elena_Kosourova,

I get your point, so that means if we want to create a dictionary with unique keys, we need to use if/else solution like problem 12, right?

If the keys are already unique, we can directly write the code that I asked before, is this right?

Thanks

Not exactly: the dictionary is always with the unique keys. The difference is if we want to have only one value related to each dictionary key (here by value I mean the dictionary value related to each dictionary key, in each key:value pair, and not the dictionary keys themselves), then we don’t need to use the if-else (just because we don’t have to create a list and to append items to it). So in this case, your code

price_to_name[price] = names 

would be perfectly fine. The resulting dictionary would look like the following example:

{200: 'D', 
 1000: 'F',
 50: 'K'}

However, in this particular task, we want to create another dictionary: where each dictionary value is a list and not a string. For example:

{200: ['D','S','U'], 
 1000: ['F'],
 50: ['K','O','E','B']}

In this case, we have to use the if-else, because we want to append different names to the same list and then use this list as a value in the corresponding key:value pair of the dictionary, where the dictionary key represents a certain price and the dictionary value - a list of names of the items with that price.

Hi @Elena_Kosourova ,

Thank you so much for your explanations, very helpful. Now I understand the logic of standard solution.

Thanks

1 Like