0

I have a dictionary that contains variables that are text. I want the dictionary to have the updated values of all the variables.However, when I update the variable, the dictionary still holds the old value.

For instance:

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

But I want the output to look like this:

First Value Key
New Value

What am I missing?

Peter Petigru
  • 207
  • 2
  • 9
  • 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,666
  • 1
  • 5
  • 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
  • 27,810
  • 13
  • 101
  • 139
Cardstdani
  • 4,999
  • 3
  • 12
  • 31