0

I have a list and a 2D array

example:

key = ['a', 'b', 'c']
value = [[1,2,3], [4,5], [6,7,8,9]]

Using this I need to create a dictionary with key as list items and values as list in 2D array.

expected dictionary is:

dicts = {'a':[1,2,3], 'b': [4,5], 'c': [6,7,8,9]}

Please help me out.

mhhabib
  • 2,733
  • 1
  • 11
  • 22
sebin
  • 63
  • 3

3 Answers3

0

you can simply do dictionary = dict(zip(key, value)), however if youd like to see how it its done with for loop have a look.

key = ['a', 'b', 'c']
value = [[1,2,3], [4,5], [6,7,8,9]]
res={}
for i in range(len(key)):
    res[key[i]] = value[i]
print(res)
0
key = ['a', 'b', 'c']
value = [[1,2,3], [4,5], [6,7,8,9]]

res = dict(zip(key, value))
print(res)
mhhabib
  • 2,733
  • 1
  • 11
  • 22
0

For the following

key = ['a', 'b', 'c']
value = [[1,2,3], [4,5], [6,7,8,9]]

A quick and easy solution is as follows:

a_dictionary={}

for i in range(len(key)):
    a_dictionary[key[i]]=value[i]
Dharman
  • 26,923
  • 21
  • 73
  • 125
BoomBoxBoy
  • 1,616
  • 1
  • 4
  • 19