0

I have a list:

['Alice', 'Bob', 'Charlie']

I want to take each of these list values and make them their own separate dictionaries:

Alice = {}
Bob = {}
Charlie = {}

I need to be able to iterate over the list and do this programatically so that it's not hard-coded, and can be done on any arbitrary list.

Is there a way to do this?

Carlos
  • 119
  • 10
  • 2
    Dynamic variable names are almost never a good idea. Instead, consider a dict of dicts like `{'Alice': {}, ...}`, for example. – j1-lee Sep 27 '22 at 20:49
  • 2
    It should almost certainly be a dictionary, not a bunch of variables: `people = {name: {} for name in ['Alice', 'Bob', 'Charlie']}`, then you access with `people['Alice']` etc. – Mark Sep 27 '22 at 20:51
  • Agreeing with other commenters. Dynamically named/referenced variables (a.k.a. "variable variables") are a huge anti-pattern and every time you think of them as a solution, think "dictionary" instead. – JNevill Sep 27 '22 at 21:03

0 Answers0