0

I have a dictionary that contains dictionaries called tick_list, as such:

(1, {'Wins: ': 0, 'WinRate: ': 0, 'Total: ': 0})
(2, {'Wins: ': 0, 'WinRate: ': 0, 'Total: ': 0})
(3, {'Wins: ': 0, 'WinRate: ': 0, 'Total: ': 0})
(4, {'Wins: ': 0, 'WinRate: ': 0, 'Total: ': 0})
(5, {'Wins: ': 0, 'WinRate: ': 0, 'Total: ': 0})

Let's say I want to add 1 to dictionary 5's Wins:. I tried:

tick_list[5]["Wins: "] += 1

But this resulted in:

(1, {'Wins: ': 1, 'WinRate: ': 0, 'Total: ': 0})
(2, {'Wins: ': 1, 'WinRate: ': 0, 'Total: ': 0})
(3, {'Wins: ': 1, 'WinRate: ': 0, 'Total: ': 0})
(4, {'Wins: ': 1, 'WinRate: ': 0, 'Total: ': 0})
(5, {'Wins: ': 1, 'WinRate: ': 0, 'Total: ': 0})

Any explanations as to why that's happening? Any how do I get to to add 1 to only 5's dictionary?

Disregard the brackets () instead of {}, I used a for loop to print the dictionary.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Eric Zhou
  • 1
  • 1
  • 1
    Are they *same dict* ? I mean you just did something like: `d = {}; a = d; a['test'] = 1; print(d)` <--- run and check the output. – Remi Guan Dec 25 '15 at 12:26
  • How do you create the tick list? Try to use a shallow copy! – fodma1 Dec 25 '15 at 12:27
  • @fodma1 THANK YOU! , I figured it out! See, I created the inner dictionary using a variable set to it, then iterating to copy that variable into my outer dictionary. This resulted in the inner dictionary being the same variable, so one change changed them all! – Eric Zhou Dec 25 '15 at 12:33

0 Answers0