-1

How to make a dictionary by using separate arrays of key and value. e.g I have:

      a = np.array([10,20,30])
      b = np.array([100,200,300])

I want a dictionary like this:

      dic = {10:100,20:200,30:300}
Rodia
  • 1,407
  • 8
  • 22
  • 29
user4129542
  • 199
  • 1
  • 1
  • 12

1 Answers1

4

dict can be constructed from a list of tuples, you can construct that list of tuples using zip:

>>> dict(zip(a,b))
{10: 100, 20: 200, 30: 300}

If you don't want to create the intermediate list (say you have two very big lists) it's better to use an iterator such as itertools.izip:

>>> from itertools import izip
>>> dict(izip(a,b))
{10: 100, 20: 200, 30: 300}
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88