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,975
  • 1
  • 15
  • 29
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)
Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18
0
key = ['a', 'b', 'c']
value = [[1,2,3], [4,5], [6,7,8,9]]

res = dict(zip(key, value))
print(res)
mhhabib
  • 2,975
  • 1
  • 15
  • 29
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
  • 30,962
  • 25
  • 85
  • 135
BoomBoxBoy
  • 1,770
  • 1
  • 5
  • 23