Python Forum
variable as dictionary name?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable as dictionary name?
#2
No, you don't dynamically create variables like that. If you want a collection of things, use a collection - you can have a list of dictionaries of course.

Of course your code does that. On line 4, you set the value of dictionary_name to an empty dict, overwriting what was in there before. Line 3 doesn't even create a variable with the name X0 for example - it just sets the value of the variable to be that string. Both lines 3 and 4 are doing the same kind of thing - assigning a value to a variable. How could the same syntax be used for that and creating variable names dynamically? It couldn't, because the interpreter wouldn't be able to guess which one you meant.
Reply


Messages In This Thread
variable as dictionary name? - by diazepam - Apr-27-2020, 04:05 AM
RE: variable as dictionary name? - by ndc85430 - Apr-27-2020, 04:08 AM
RE: variable as dictionary name? - by diazepam - Apr-27-2020, 04:25 AM
RE: variable as dictionary name? - by ndc85430 - Apr-27-2020, 04:31 AM
RE: variable as dictionary name? - by deanhystad - Apr-27-2020, 08:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reference new dictionary keys with a variable slouw 4 2,946 May-07-2019, 03:30 AM
Last Post: slouw
  Dictionary named after variable value Alfred 1 3,079 Sep-02-2017, 01:24 PM
Last Post: sparkz_alot
  Build a multi-variable dictionary birdieman 4 5,066 Jan-06-2017, 04:34 PM
Last Post: birdieman
  calling an object variable in a dictionary sunhear 3 4,343 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020