0

Suppose you have two lists of strings, the first one being the keys for the dictionary, and the second being the values for the keys of corresponding position. So it's something like this:

>>print keys
['key1', 'key2', ..., 'keyN']
>>print values
['value1', 'value2', ..., 'valueN']

with this line of code:

dic = dict.fromkeys(keys)

I've assigned the keys to my dictionary. So my output is something like this now:

>>print dic
{'key1': None, 'key2':None, ..., 'keyN': None}

Can I somehow do the same thing I did with the keys, but with the values as well? But only this time store the values one by one to the dictionary? So my targeted output is something like this:

>>print dic
{'key1':'value1', 'key2':'value2', ..., 'keyN':'valueN'}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
Mixalis
  • 532
  • 5
  • 17

1 Answers1

5

Use zip:

>>> keys = ['a', 'b', 'c']
>>> values = ['1', '2', '3']
>>> dict(zip(keys, values))
{'a': '1', 'c': '3', 'b': '2'}
Bahrom
  • 4,752
  • 32
  • 41
  • no, your code works fine! it was just a mis-understanding on my part from the output I got from my code, It changed the order of 1 pair of keys. Nothing special :) Sorry for the trouble. – Mixalis Aug 11 '16 at 15:34
  • Ah, got it, no worries – Bahrom Aug 11 '16 at 15:41