How to convert tuple of arrays to a dict?

arrays, dictionary, python, python-3.x, tuples

Solution

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}

Problem

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.

Original source

Related problems