1011 #12: Laptop| Question about conditional dictionary search

Screen Link:

My Code:

laptops = list(reader(open("laptops.csv")))
laptops = laptops[1:]
price_to_name = {}

for rows in laptops:
    pcs = []
    price = int(rows[2])
    for row in laptops:
        pc = row[1]
        if price == int(row[2]):
    price_to_name[price] = pcs

laptop1 = []
laptop2 = []

for key in price_to_name:
    for price in price_to_name:
        if (price + key == 5000 and price != key):
            laptop1 = price_to_name[key]
            laptop2 = price_to_name[price]

What I expected to happen:
I wanted the for loops to search the keys for a combination where the two keys were not equal and the price adds up to 5000. Once it found those keys it would copy the laptop portion of the dictionary to laptop1 and 2.

What actually happened:

list (<class 'list'>)
list (<class 'list'>)

My output is weird because whenever I change the number I want the keys to add up to I get a list with the correct answer. For example, this is the output with 3400 instead of 5000.
list (<class ‘list’>)
[‘Thinkpad L560’]
list (<class ‘list’>)
[‘Rog G752VS-BA171T’]
So I thought there were no solutions to the problem but when I copy and pasted the answer code it worked. Any help on this would be greatly appreciated.

Click here to open the screen in a new tab.

1 Like
from csv import reader
from collections import defaultdict 
price_to_name = defaultdict(list) 

csv_reader = reader(open("laptops.csv"))

# Ignore the first line of header

# Loop through from second line onwards
for row in csv_reader:
    brand, name, price = row
    price = int(price)

total = 5000

possible_laptop1_values = [value for value in sorted(price_to_name.keys()) if value < total]

result = [] 

# Using complement (or subtraction) to figure out laptop 1 and 2 values 
# Know laptop1_value, using total = laptop1_value + laptop2_value 
# laptop2 _value = total - laptop1_value

for laptop1_value in possible_laptop1_values:

    laptop2_value = total - laptop1_value 
    if laptop2_value in price_to_name:

        if laptop1_value != laptop2_value:
            laptop1 = price_to_name[laptop1_value][0]
            laptop2 = price_to_name[laptop2_value][0]

        elif len(price_to_name[laptop1_value]) > 1:
           # Same value for laptop1 and laptop2, check if there are at least two different values
            laptop1 = price_to_name[laptop1_value][0]
            laptop2 = price_to_name[laptop2_value][1] 
        result.append((laptop1, laptop2))


The output prints

laptop1="Laptop MSI"
laptop2="GS60 Ghost"

You did not account for when both latop1 and laptop2 has the same value. That is, negation of price == key, which is price != key in your code. You can remove this comparison because both == and != are required.

The comparison only matters if both have the same value, then you need to return different laptop names. Otherwise, you can assume there is at least 1 unique name in price_to_name for that price.

        if (price + key == 5000 ):

            if laptop1_value != laptop2_value:
               laptop1 = price_to_name[price][0]
               laptop2 = price_to_name[key][0]

        elif len(price_to_name[laptop1_value]) > 1:
             # Same value for laptop1 and laptop2, check if there are at least two different values
              laptop1 = price_to_name[price][0]
              laptop2 = price_to_name[key][1]