-2

I am trying to convert multiple list into dictionary as in below output in python.

defined variables

a = ['23820', '29989']
i = ['hello', 'fdhcsl']
u = ['bye', 'cya']

Expected output: (nested dictionary)

nest = {'23820':{'i' : 'hello', 'u': 'bye' }, '29989':{'i': 'fdhcsl', 'u' : 'cya'}}

Is there is anyway this can by achieved by any means?

Thanks

sam
  • 7
  • 2

1 Answers1

0

This can be achieved by the following code:

a = ['23820', '29989']
i = ['hello', 'fdhcsl']
u = ['bye', 'cya']
nest = {}
for x,c in enumerate(a):
    nest.update({c:{'i':i[x],'u':u[x]}})
print(nest)
zettatekdev
  • 106
  • 4
  • You can make this cleaner by using `zip` instead of `enumerate`, and a comprehension instead of a for-loop: `{c: {'i': d, 'u': e} for c, d, e in zip(a, i, u)}` – wjandrea Sep 02 '20 at 16:45