Skip to content
Advertisement

Convert List of list into tuple of tuples

I have a list of 2x1 matrices like:

[[[2.3], [2.4]], [[1.7], [1.6]], [[2.02], [2.33]]]

I need to convert it into a tuple of tuples, like:

((2.3,2.4),(1.7,1.6),(2.02,2.33))

I know I can loop through the list and convert it manually , trying to check if there is a better-optimized way of doing it.

Advertisement

Answer

You can do it this way using numpy indexing and slicing that outer dimension.

ma =  [[[2.3], [2.4]], [[1.7], [1.6]], [[2.02], [2.33]]]
ama=np.array(ma) #incase it wasn't a numpy array since you mentioned numpy in tags
tuple(map(tuple,ama[:, :, 0].tolist()))

Output:

((2.3, 2.4), (1.7, 1.6), (2.02, 2.33))
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement