1

I have a set of keys, lets say

mykeys = ["I", "II", "III", "IV"]

and a set of values, let's say

myvalues = [1, 2, 3, 4]

I now want to create a dictionary like this

{"I": 1, "II": 2, "III":3, "IV":4}

What is the easy (and, if possible, idiomatic) way to do it in python?

Karel Bílek
  • 36,467
  • 31
  • 94
  • 149

1 Answers1

1
d = dict(zip(mykeys, myvalues))

zip sticks the two lists together, so you get something like [("I", 1), ("II", 2"), etc] and dict converts that to a dictionary where the first part of each tuple is the key and the second part of the tuple is the value.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188