1

I have two Python lists. One list, called area, stores the areas of homes. Different homes may have the same area. Another list, called price, stores the corresponding prices of these homes. I want to create a dictionary called priceDict, whose keys are the unique areas of homes and whose values are lists of the prices of homes of the same area. For example, suppose area = [1500, 500, 1500, 2000, 2500, 2000], price = [30000, 10000, 20000, 40000, 50000, 45000]. I would like to create the dictionary priceDict = {1500: [30000, 20000], 500: [10000], 2000: [40000, 45000], 2500: [50000]}.

I wrote the following Python code:

area = [1500, 500, 1500, 2000, 2500, 2000]
price = [30000, 10000, 20000, 40000, 50000, 45000]
priceDict = dict.fromkeys(area,[])
    
for houseArea in priceDict:
    for areaIndex in range(len(area)):
        if area[areaIndex] == houseArea:
            priceDict[houseArea].append(price[areaIndex])

print(priceDict)

However, when I executed the code, the output I got was:

{1500: [30000, 20000, 10000, 40000, 45000, 50000], 500: [30000, 20000, 10000, 40000, 45000, 50000], 2000: [30000, 20000, 10000, 40000, 45000, 50000], 2500: [30000, 20000, 10000, 40000, 45000, 50000]}

I would like to ask what's wrong with my code and how I can re-write my code so that it performs the intended function.

  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) `[some_list] * number` and `dict.fromkeys(iterable, some_list)` contain the same problem. – Mechanic Pig Sep 28 '22 at 16:46
  • What is the pattern to get these values: `priceDict = {1500: [30000, 20000], 500: [10000], 2000: [40000, 45000], 2500: [50000]}`. I can understand if the first element in one list is the key and the other first element of the other list is the value. But you are assigning 2 values to 1 key. How we know the pattern of your pair key-values? – Luis Alejandro Vargas Ramos Sep 28 '22 at 16:51
  • @LuisAlejandroVargasRamos For example, the elements with indices 0 and 2 in list ```area``` have the value 1500, therefore the elements with indices 0 and 2 in list ```price```, which are 30000 and 20000, are put into the same list that serves as the value of the key ```1500``` in the dictionary ```priceDict```. – YuanLinTech Sep 28 '22 at 16:57

1 Answers1

3

Your first problem is here:

priceDict = dict.fromkeys(area,[])

This makes every key in priceDict share the same list.

The second problem is you are nesting for-loops rather than using a single for-loop and zipping to pair up your items:

priceDict = {}
for k, v in zip(area, price):
    priceDict.setdefault(k, []).append(v)

Note that priceDict.setdefault(k, []).append(v) is slightly inefficient because it creates an empty list on every iteration. A more efficient way is to use a defaultdict of list.

from collections import defaultdict

priceDict = defaultdict(list)
for k, v in zip(area, price):
    priceDict[k].append(v)
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119