-1
d={'a0': [['5021', '5031'], ['4994', '4991', '5042'],
          ['4992', '4995', '5021', '4994'], ['5037', '5038']],
   'a24': [['5009', '5014'], ['5009', '5014'], ['4993', '4998', '5030', '4991']]
}

I have the above dict in python. I need to make list with the name of it being the names of keys in the dict. The list with the name of the key should have the items as its corresponding values in dict.

The output should be:

a0=[['5021', '5031'], ['4994', '4991', '5042'],
              ['4992', '4995', '5021', '4994'], ['5037', '5038']]
a24=[['5009', '5014'], ['5009', '5014'], ['4993', '4998', '5030', '4991']]

Any help is appreciated.

2 Answers2

0

First, rethink if this is really necessary. Dynamically creating variables is confusing and does not occur often. It would be better to avoid this.

However, you can do it like this:

for name, val in d.items():
    exec("{}={}".format(name,val))
Tristan
  • 2,000
  • 17
  • 32
-1
for key, item in d.items():
    print(str(key) + "=" + str(item))
Sam
  • 454
  • 4
  • 18
  • 3
    a verbal explanation is often helpful – con Apr 04 '20 at 17:11
  • This does not answer the question, which was how to dynamically create variables. – Gino Mempin Apr 05 '20 at 03:42
  • That's not what the question says and I'm not psychic. The output matched the questions desired output. – Sam Apr 05 '20 at 08:06
  • The OP mentions this in [the comment](https://stackoverflow.com/questions/61026570/dictionary-converted-to-array-in-python/61026611#comment107964913_61026570): "*how do i dynamically create a list with the name of the key in the dict?*". The desired output is `a0` and `a24` are the *variable* names of the lists extracted from the dict, not simply printing key=value pairs to the console. I admit though that the question really wasn't that clear enough. – Gino Mempin Apr 05 '20 at 08:24
  • As I mentioned before, I am not pshycic. I answered the question as it was when I answered and cannot open a time portal and view extra information the OP adds after the fact. When I answered the desired output was the only solid description of what was needed. – Sam Apr 05 '20 at 08:29