-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,383
  • 7
  • 20
  • 28
user4129542
  • 169
  • 1
  • 1
  • 11

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
  • 29,003
  • 5
  • 68
  • 85