0

My code looks like this:

firstValue = "First Value Key"
secondValue = "Second Value Key"
thirdValue = "Third Value Key"

Dictionary = {"0":firstValue,
              "1":secondValue,
              "2":thirdValue}

print(Dictionary["0"])

firstValue = "New Value"

print(Dictionary["0"])

The output is the following:

First Value Key
First Value Key

And I want the output to look like this:

First Value Key
New Value

What am I missing?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Peter Petigru
  • 101
  • 1
  • 7
  • The dictionary contains a reference to the _value_, not the _name_. Recommended reading: https://nedbatchelder.com/text/names.html – jonrsharpe Oct 31 '21 at 17:06

2 Answers2

-1

You're only changing the variable that you already passed into the dictionary, update the value directly to it:

Dictionary['0'] = "New Value"
Pedro Maia
  • 2,590
  • 1
  • 4
  • 20
-1

You need to assign the value of the variable to the dictionary value:

firstValue = "First Value Key"
secondValue = "Second Value Key"
thirdValue = "Third Value Key"

Dictionary = {"0":firstValue,
              "1":secondValue,
              "2":thirdValue}

print(Dictionary["0"])

firstValue = "New Value"
Dictionary["0"] = firstValue
print(Dictionary["0"])
E_net4 - Krabbe mit Hüten
  • 24,143
  • 12
  • 85
  • 121
Cardstdani
  • 2,044
  • 7
  • 25