0

I initialized a list of dictionaries using:

list = [{}] * 5

And when I modify the values within, for instance:

list[3]["key"] = 2
list[4]["key"] = 5

printing those 2 values will both get me 5.

Is there a solution to this? I'm trying to create a list of 5 dictionaries, holding 2 keys and 2 values but I have no idea how to initialize that.

Daryl Lim
  • 13
  • 3
  • Beside the point, but `list` is a bad variable name since it [shadows](https://en.wikipedia.org/wiki/Variable_shadowing) the [builtin `list` type](https://docs.python.org/3/library/stdtypes.html#list). Use a more descriptive name, or at least something like `lst`. For an example, see [TypeError: 'list' object is not callable in python](/q/31087111/4518341). BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Aug 26 '21 at 22:04
  • Oh yeah I'm not actually using list as the variable name, just showing it's a list. Thanks! – Daryl Lim Aug 26 '21 at 22:17

0 Answers0