Convert numpy array to tuple

Go To


Note: This is asking for the reverse of the usual tuple-to-array conversion.

I have to pass an argument to a (wrapped c++) function as a nested tuple. For example, the following works

X = MyFunction( ((2,2),(2,-2)) )

whereas the following do not

X = MyFunction( numpy.array(((2,2),(2,-2))) )
X = MyFunction( [[2,2],[2,-2]] )

Unfortunately, the argument I would like to use comes to me as a numpy array. That array always has dimensions 2xN for some N, which may be quite large.

Is there an easy way to convert that to a tuple? I know that I could just loop through, creating a new tuple, but would prefer if there's some nice access the numpy array provides.

If it's not possible to do this as nicely as I hope, what's the prettiest way to do it by looping, or whatever?

2012-04-04 17:33
by Mike


>>> arr = numpy.array(((2,2),(2,-2)))
>>> tuple(map(tuple, arr))
((2, 2), (2, -2))
2012-04-04 17:35
by Niklas B.
So for a simple 1-d array tuple(arr)FindOutIslamNow 2019-02-05 11:46


Here's a function that'll do it:

def totuple(a):
        return tuple(totuple(i) for i in a)
    except TypeError:
        return a

And an example:

>>> array = numpy.array(((2,2),(2,-2)))
>>> totuple(array)
((2, 2), (2, -2))
2012-04-04 17:52
by Bi Rico
Nice generalization. As a python newbie, though, I wonder if it's considered good style to use exceptions for a condition that is almost as common as the non-exceptional state. At least in c++, flow control by exceptions is usually frowned upon. Would it be better to test if type(a)==numpy.ndarray - Mike 2012-04-05 15:36
This is pretty common in python because of the concept of "duck-typing" and EAFT, more here: The advantage of this approach is that it'll convert any nested sequence into nested tuples, not just an array. One thing I should have done that I've fixed is specify which errors I want handled by the except block - Bi Rico 2012-04-05 16:53
In C++, exceptions are slow relative to conditionals for a variety of reasons. In Python, they are approximately the same in terms of performance - this is one of the places that we have to check our C++ intuitions at the door - dbn 2015-11-19 23:02


I was not satisfied, so I finally used this:

>>> a=numpy.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

>>> tuple(a.reshape(1, -1)[0])
(1, 2, 3, 4, 5, 6)

I don't know if it's quicker, but it looks more effective ;)

2013-08-12 13:35
by hoping it helps
That was not the shape that I wanted for the tuple - Mike 2013-08-12 18:32


Another option

tuple([tuple(row) for row in myarray])

If you are passing NumPy arrays to C++ functions, you may also wish to look at using Cython or SWIG.

2014-05-01 18:50
by Greg von Winckel
That doesn't convert to tuple. Converts to a list - Peter 2015-09-23 04:54
Did you try it? It makes a tuple when I run in. Note that the last function called is tuple, which returns a tuple. If you have only the [...] part without the outer tuple, you will get a list of tuples - Greg von Winckel 2015-09-24 14:26
Hmm sorry about that; you're right. I was a bit sleepy last night and saw a list returned so yeah, it must have been a list with a tuple inside - Peter 2015-09-24 16:01