Hi @vtwinhead, welcome to the community!
The way that the price_to_name
dictionary was created in the solution ends up giving you dictionary items where the key is the price and the value of the key is a list that contains one or more laptop names. I’ll print a few of them just for visual aids, because I think it’s important to understand how the dictionary looks to understand the if/elif statements:
{174: ['C740-C9QX (3205U/2GB/32GB/Chrome'],
192: ['Vivobook E200HA'],
196: ['V131 (X5-Z8350/4GB/32GB/FHD/W10)'],
199: ['E402WA-GA010T (E2-6110/2GB/32GB/W10)', 'Chromebook C910-C2ST'],
203: ['K146 (N3350/4GB/32GB/W10)'],
209: ['Chromebook 15', 'Stream 11-Y000na'],
...
Let’s look at the 209 key. price_to_name[209]
has the value of ['Chromebook 15', 'Stream 11-Y000na']
. If I wanted to access just one of these laptop names, I need to use the index value. That means:
price_to_name[209][0] = 'Chromebook 15'
and
price_to_name[209][1] = 'Stream 11-Y000na'
Additionally, this would mean that len(price_to_name[209]) = 2
, because there were 2 items in the list.
For key 192
, price_to_name[192]
has a value of ['Vivobook E200HA']
. This means that price_to_name[192][0] = 'Vivobook E200HA'
.
So now let’s look at the if/elif statements.
The first scenario is the possibility that we have 2 laptops that both have a price of 2500. The if statement is checking for 2 things: first, that the price is 2500 for that row in the dataset, and secondly if there are 2 or more laptops at that price from the dictionary. If both conditions pass, then we can assign the laptop
variables to those values.
The second scenario would be to find 2 laptops with different prices but add to 5000. Let’s say price = 2300
– then we’re looking in the dictionary to see if the key 2700
(5000-2300) exists in the dictionary. If it does, then that means laptop1 = price_to_name[2300][0]
and laptop2 = price_to_name[2700][0]
. (We use [0]
for both, because even if there are 2 laptops at either price, we only need one of them and every key has at least 1 item in the list.)
I hope that helps!