convert 3 dimensional array to 2 dimensional python code example

Example 1: python convert two dimensional list to one dimensional

# Python convert 2D into 1D array:

import itertools
x = [['foo'], ['bar', 'baz'], ['quux'], ("tup_1", "tup_2"), {1:"one", 2:"two"}]
print list(itertools.chain(*x))
print [element for sub in x for element in sub]

# Output:

['foo', 'bar', 'baz', 'quux', 'tup_1', 'tup_2', 1, 2]

Example 2: python convert multidimensional array to one dimensional

In [12]: a = np.array([[1,2,3], [4,5,6]])

In [13]: b = a.ravel()

In [14]: b
Out[14]: array([1, 2, 3, 4, 5, 6])