12. Laptop Purchase Question

Screen Link: https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/12/laptop-purchase

The question is to retrieve the price of laptops to see which ones have a price exactly equal to 5000/ (The logic is sound, however the syntax has baffled me, which is why I ask for some assistance)

The answer to this question shows: ( I have added my doubts to the solution inside the code itself)

price_to_name = {}
for row in rows:
price = int(row[2])
name = row[1]
if price in price_to_name:
price_to_name[price].append(name)

#DOUBT - How do we append to a dictionary? I understand that maybe price could be a list, however where and how to define it and be able to append to the name list inside of ‘price to name’? or is it predefined and it just takes our word(code) for it when we try to append something to it?

else:
    price_to_name[price] = [name]

initialize the two laptops in the answer to None

laptop1 = None
laptop2 = None

look for a solution

for row in rows:
price = int(row[2])
# this laptop’s cost is price, so if there is a laptop that
# costs 5000 - price then their total price is exactly 5000
if price == 2500 and len(price_to_name[2500]) >= 2:
laptop1 = price_to_name[2500][0]
laptop2 = price_to_name[2500][1]
elif 5000 - price in price_to_name:
laptop1 = price_to_name[price][0]
laptop2 = price_to_name[5000 - price][0]
print(price)

print the solution

print(laptop1)
print(laptop2)

Hello @swar.joshi Welcome to the DataQuest Community.

Note that several laptops can have the same price , your solution needs to take this into account. key is the price of the laptop, and the value is the list of all laptop names with that price (there can be more than one laptop with a given price).

I understand that maybe price could be a list ? -> No , Price is an element selected from the row

see this : price = int(row[2])
(You need to check file laptops.csv)

==========================
Understand below code

if price in price_to_name:
price_to_name[price].append(name)
else:
price_to_name[price] = [name]

if price (element) already exist in price_to_name (dictionary) as Key .
*it will goto case 1 -> price_to_name[price].append(name) *
i.e, updating the value of price .

*else if : if price (element) do not already exist in price_to_name (dictionary) as Key *
it will goto case 2 -> price_to_name[price] = [name]
i.e, appending a new key ’ price ’ with it’s value.
(This is how you append a dictionary -> If Key already exist then update it’s value else add key - value pair)

1 Like

Thank you for your reply!
I understand it now. I actually got confused between appending to the dictionary and appending to an element in the dictionary. Because it was mentioned that it is not possible to append a dictionary like we append lists. Hence the confusion.

This clears everything up for me. Thank you very much! :slight_smile:

This might be a more intuitive way to do that:

Populate the dictionary with keys corresponding to the names of the laptop and values corresponding to their price. And then reverse that dictionary using the zip function. You have the same response, except the names in this case are not stuck in those lists in the dictionary.

name_to_price={}
for i in laptops_data:
(name_to_price[i[1]])=int(i[2])

price_to_name = dict(zip(name_to_price.values(), name_to_price.keys()))

What I’m having a hard time doing now is actually the second part after we intialize the two laptops to none. Can anybody help explain what is going on there.

if price == 2500 and len(price_to_name[2500]) >= 2:
laptop1 = price_to_name[2500][0]
laptop2 = price_to_name[2500][1]
elif 5000 - price in price_to_name:
laptop1 = price_to_name[price][0]
laptop2 = price_to_name[5000 - price][0]

2 Likes