3

I have the following tuple:

t = (array([0, 1, 2, 3], dtype=uint8), array([1568726,  346469,  589708,   91961]))

that I'd need to convert to a dict as follows:

dict = {0: 1568726, 1: 346469, 2: 589708, 3: 91961}

I'm trying with

d = dict((x, y) for x, y in t)

but it's not resolving the nesting of the tuple I have. Any suggestions?

Another SO question appears to be similar, but is not: its main issue is re transposing the dict elements, while this question focuses on how to join 2 arrays within a tuple into a dict.

Community
  • 1
  • 1
pepe
  • 9,799
  • 25
  • 110
  • 188

1 Answers1

6

You can use zip (to create key-value pairs) and dict (to convert the pairs to a dictionary):

>>> from numpy import array, uint8
>>> t = (array([0, 1, 2, 3], dtype=uint8),
         array([1568726,  346469,  589708,   91961]))
>>> dict(zip(*t))
{0: 1568726, 1: 346469, 2: 589708, 3: 91961}
falsetru
  • 357,413
  • 63
  • 732
  • 636