0

I have two different list:

key = ['key1', 'key2', 'key3']
value = ['value1', 'value2', 'value3']

I want to map it like this :

dictionary = {'key1':'value1',
              'key2':'value2',
              'key3':'value3'}

I tried this:

dictionary = {key, value}

But I got this result:

TypeError: dict expected at most 1 arguments, got 2


            
Chau Loi
  • 1,106
  • 1
  • 14
  • 36
  • Please search on stack overflow before asking: https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary – Harsha Biyani Nov 19 '20 at 07:26

3 Answers3

7

Do this to get the desired result,

dictionary = dict(zip(key, value))
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
1

Try this :

dictionary = {key[i]:value[i] for i in range(len (key))}

This is what is called a dictionary comprehension.

Essentially what you are doing is looping through the indexes and accessing the various values at the dictionary.

This might help

Benjamin Philip
  • 178
  • 1
  • 11
0
res = {key[i]: value[i] for i in range(len(key))} 
Carl H
  • 249
  • 3
  • 12