0

I am currently trying to create a dictionary named based on a variable. But the given code only returns the following error: "SyntaxError: cannot assign to operator".

Is this idea inherrently flawed, or is there a possible solution for this issue?

a = 1 
'rmsettings'+str(a) = {}
Thamsen
  • 23
  • 4
  • 2
    Glad you asked about inherently flawed! This seems like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Use a list if you want "dynamic" variables in 0..n slots: `rmsettings = [{}]` then use `rmsettings[0]` to access the object, or `rmsettings[a]` to get the `a`-th object (1 would be out of bounds in this case since lists are 0-indexed). If you want non-sequential, named or complex keys, use a dict of dicts. – ggorlen Jun 12 '20 at 16:14
  • 2
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – ggorlen Jun 12 '20 at 16:17
  • 1
    It does! I might have to use a different solution for what i am trying to do. Thank you! – Thamsen Jun 12 '20 at 16:25

0 Answers0