0

I am writing a script supporting users in FE analysis. During the runtime of the script I get a list of FEA entities like this

Ents=['BAR','SHELL','BEAM']

I have to create a dictionary for each item in the list to collect and store the correct values from another source and the number of possible FEA entities is long and may change if a new entity is introduced.

Is there a way to create a dictionary "dynamically during the runtime" (I have no better idea to describe my request) like

for item in Ents:
    item+'_dict' = dict()

So as a result I get 3 dicts for the example above: BAR_dict, SHELL_dict and BEAM_dict

And for

Ents = ['TRUSS','WELD','CONNECTOR']

I get a dict named TRUSS_dict, WELD_dict and CONNECTOR_dict

  • 4
    Why not store all dicts in a "super-dict", like `dicts = dict(); dicts[item+'_dict'] = dict()`? – konvas Aug 07 '18 at 07:48
  • 1
    You may also have a dict of dict using dict comprenhension: `Ents_dict = {e: {} for e in Ents}` – DFE Aug 07 '18 at 07:49
  • 1
    Using dynamic variables is generally considered an anti-pattern in Python. Rather, you should use some sort of *container*. The type you choose depend son your needs. In this case, you seem to want to access your data by using a *string*, so *another* dict is a natural choice. – juanpa.arrivillaga Aug 07 '18 at 07:50

1 Answers1

0

Try this:

EntDicts = {}
for Ent in Ents:
    EntDicts[Ent] = {}

Then use EntDicts at will...

goodvibration
  • 5,980
  • 4
  • 28
  • 61