0

I have a dictionary where the value is a variable. When I update the dictionary based on some condition, the 'dictionary element' is updated, but not the referred variable

My code is something like

my_bool = True
my_count_dictionary={'name': my_bool}
my_count_dictionary['name'] = not my_count_dictionary['name'] #Need to toggle it
print (my_count_dictionary['name'])
print (my_count)

I am expecting the output like

False
False

but i am getting

False
True

The original variable is still not updated. I am from C background. I guess i am expecting something like a reference here. Is it possible in python?

  • `+=` does not work in-place here, despite the syntax. Integers are immutable so a new value is returned and the object that `my_count` references is left untouched – roganjosh Dec 19 '19 at 12:04
  • 1
    Does this answer your question? [Passing an integer by reference in Python](https://stackoverflow.com/questions/15148496/passing-an-integer-by-reference-in-python) – Simon Crane Dec 19 '19 at 12:05
  • [Possibly related](https://stackoverflow.com/questions/10205469/referencing-and-setting-variables-in-python-dicts) – LorenzBung Dec 19 '19 at 12:05
  • Infact i need it for a boolean type – Ajay Joseph Dec 19 '19 at 12:30

0 Answers0