I am trying to get a list of objects with updated value for a key with each iteration. I need this done for each dict in a list of dicts.
I tried to do this by iterating through each color, and within each color iteration, nest a loop for each dict in the dict list, and use the update method for the new added key.
But I keep getting the very last element of Green in the color list for every color key value. I need objects for each fruit and each color. I know it's looping through each color and uses the last one, but why does the append method not add to the outside list before being overwritten? How do I write it so that I can get each color for each key in the end list? There must be something I'm missing here. I'm a beginner so will take any help I can get.
end_lst=[]
color_lst=['Blue','Red','Purple','Green']
fruit_lst = [{'fruit':'apple'}, {'fruit':'pear'}, {'fruit':'mango'}]
for color in color_lst:
for fruit in fruit_lst:
fruit.update({'color': color})
end_lst.append(fruit)
print(end_lst)
#printed result:
[{'fruit': 'apple', 'color': 'Green'}, {'fruit': 'pear', 'color':
'Green'}, {'fruit': 'mango', 'color': 'Green'}, {'fruit': 'apple',
'color': 'Green'}, {'fruit': 'pear', 'color': 'Green'}, {'fruit':
'mango', 'color': 'Green'}, {'fruit': 'apple', 'color': 'Green'},
{'fruit': 'pear', 'color': 'Green'}, {'fruit': 'mango', 'color':
'Green'}, {'fruit': 'apple', 'color': 'Green'}, {'fruit': 'pear',
'color': 'Green'}, {'fruit': 'mango', 'color': 'Green'}]
#What I am needing:
[{'fruit': 'apple', 'color': 'Blue'}, {'fruit': 'pear', 'color':
'Blue'}, {'fruit': 'mango', 'color': 'Blue'}, {'fruit': 'apple',
'color': 'Red'}, {'fruit': 'pear', 'color': 'Red'}, {'fruit': 'mango',
'color': 'Red'}, {'fruit': 'apple', 'color': 'Purple'}, {'fruit':
'pear', 'color': 'Purple'}, {'fruit': 'mango', 'color': 'Purple'},
{'fruit': 'apple', 'color': 'Green'}, {'fruit': 'pear', 'color':
'Green'}, {'fruit': 'mango', 'color': 'Green'}]
CodePudding user response:
In iteration of dicts you are getting same dict from your list every time when you assign new key 'color' to it, that's why you get the last color in dicts you append in result list. When you add dict object in result list you are not creating new dict, but you are creating new link to dict in result list. So when you do fruit.update({'color': color}) you actually update dict not only in current iteration but also in result list, because fruit and dictionary added to result list from previous iteration is the same object. To achieve result you want you need to do copy of the dict in iteration (so it will be new object) then update this copy and add in the result list:
end_lst=[]
color_lst=['Blue','Red','Purple','Green']
fruit_lst = [{'fruit':'apple'}, {'fruit':'pear'}, {'fruit':'mango'}]
for color in color_lst:
for fruit in fruit_lst:
fruit_copy = fruit.copy()
fruit_copy.update({'color': color})
end_lst.append(fruit_copy)
print(end_lst)
CodePudding user response:
@VFarsiyants - I forgot about that particular behaviour - that python was not actually creating a copy, kudos to you.
Just because I spent so long trying to work out the answer an alternative would be:
new_fruit = {'fruit': fruit['fruit'], 'color': color}
instead of the two lines above to create a new dictionay. I don't think it is particulary 'better' just a different approach to the same result.