1

I have created 4 different sets of dictionaries with the guidance of this post: Python variables as keys to dict. I now want to merge all these dictionaries into 1 list. I tried the following:

classes = ['apple', 'orange', 'pear', 'mango']
class_dict = {}
store = []

for fruit in classes:
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

The output is as shown below. As you can see, store only contains a list of the same dictionary being appended to it each time. I'm not sure where I'm going wrong and some help on this would be much appreciated!

{'fruit': 'apple', 'o': 0, 'q': 0}
{'fruit': 'orange', 'o': 2, 'q': 1}
{'fruit': 'pear', 'o': 0, 'q': 0}
{'fruit': 'mango', 'o': 0, 'q': 0}

store:  [{'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}]
peru_45
  • 330
  • 3
  • 16
  • I cant understand what you have tried to achieve. Please add the required output for `print(store)` – Green Jan 12 '22 at 20:03
  • It's already been added. Please see the bottom of my question. – peru_45 Jan 12 '22 at 20:05
  • @peru_45 You deleted your other question about three seconds before I could click "Post your answer". Here is what I had written: https://pastebin.com/ArJxA2X1 – Stef Jan 13 '22 at 16:41
  • @Stef Thank you for your answer; it helped me. I undeleted my question if you wish to post it on there. – peru_45 Jan 13 '22 at 17:07

2 Answers2

0

You need to move your class_dict inside the loop.

classes = ['apple', 'orange', 'pear', 'mango']

store = []

for fruit in classes:
    class_dict = {}
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

output:

{'fruit': 'apple', 'o': 0, 'q': 0}
{'fruit': 'orange', 'o': 2, 'q': 1}
{'fruit': 'pear', 'o': 0, 'q': 0}
{'fruit': 'mango', 'o': 0, 'q': 0}
store:  [{'fruit': 'apple', 'o': 0, 'q': 0}, {'fruit': 'orange', 'o': 2, 'q': 1}, {'fruit': 'pear', 'o': 0, 'q': 0}, {'fruit': 'mango', 'o': 0, 'q': 0}]
JMad
  • 112
  • 1
  • 7
0

You should just move class_dict inside the loop:

classes = ['apple', 'orange', 'pear', 'mango']

store = []

for fruit in classes:
    class_dict = {}
    if fruit == "orange":
        o = 2
        q = 1
    else:
        o = 0
        q = 0

    for j in ('fruit', 'o', 'q'):
        class_dict[j] = locals()[j]
    print (class_dict)
    store.append(class_dict)
print ("store: ", store)

It's because dict is a mutable object in python and in every iteration of your for loop you change the value of the global variable class_dict. The simple example of it:

>>> a = {'a': 1, 'b': 2}
>>> a
{'a': 1, 'b': 2}
>>> b = a
>>> b
{'a': 1, 'b': 2}
>>> b['c'] = 3
>>> b
{'a': 1, 'b': 2, 'c': 3}
>>> a
{'a': 1, 'b': 2, 'c': 3}

When you move class_dict inside the loop, this variable becomes local and iterations of the loop become independent.

Oleksii Tambovtsev
  • 2,666
  • 1
  • 3
  • 21