-1

I have the keys as key1= [(0,0,0), (0,0,1),(1,0,0), (0,1,0)]

  for x1,x2,x3 in key1:
        
        summat= #some expression that has new value for each keys
        tempDict ={(x1,x2,x3): summat}
  print(tempDict)

It only prints the last value(0,1,0) and the summat of last value but I want to store all the values of summat for all the value of keys

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • Welcome to StackOverflow. Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), so we can run your code and see what's wrong. Right now it's quite unclear what you have in mind and what's going wrong. – Jack Avante Mar 18 '22 at 22:31
  • 2
    Does this answer your question? [How can I add new keys to a dictionary in Python?](https://stackoverflow.com/questions/1024847/how-can-i-add-new-keys-to-a-dictionary-in-python) – kcsquared Mar 18 '22 at 22:32
  • Do you want to add the values of the dictionary together? – Blue Robin Mar 18 '22 at 22:36

1 Answers1

2

Try replacing what you have into a dictionary comprehension:

tempDict = {key: summat(key) for key in key1}

This is the equivalent to:

tempDict = {}
for key in key1:
   tempDict.update({key: summat(key)}

Your code will overwrite the value of tempDict each iteration, so at the end you'll only see the last value.