# Python Program to get the flattened 1D array

An array is a data structure, which is used to store a set of homogeneous data elements. And it can have more than one dimension.

1D array −

[1 2 3 4 5 6]


2D array −

[[1 2 3]
[4 5 6]
[7 8 9]]


Flattening an array means reducing the dimensionality of a Multi-dimensional array.

In the article below we will discuss the python program to get the flattened 1D array. Here we will use List and NumPy arrays to represent a normal array. Because python does not have a native data structure for arrays.

## Input Output Scenarios

Assume we have a 2-Dimensional array as an input. And the output will be the flattened array.

Input array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Output:
Flattened array:  [1 2 3 4 5 6 7 8 9]


## Using a nested list

From the python functools module we can use the reduce function to flatten the 2-dimensional array. reduce() function is used to apply a specified function to the items of the sequence and it will return a reduced sequence. Following is the syntax to do so –

reduce(function, iterable[, initializer])


Where,

• function is a predefined function that applies on items of iterable.

• Iterable any python iterables ex: list, tuple, string, and dictionary.

To use the reduce function then we need to import it from the functools module.

### Example

In this example, we will get the flattened 1d array by using lambda and reduce () function.

from functools import reduce

arr_2d = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# print initial array
print("Original array: ", arr_2d)

# flattening the 2d array into 1d array
# using reduce function
flattened_arr = reduce(lambda x,y:x+y, arr_2d)

print("Flattened array: ", flattened_arr)


### Output

Original array:  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened array:  [1, 2, 3, 3, 6, 7, 7, 5, 4]


The reduce function successfully flattened the 2D array with the help of lambda function.

### Example

Also, we can use list comprehension to get the flattened array. Let's see the example below.

arr_2d = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]

# print initial array
print("Original array: ", arr_2d)

# flattening the 2d array into 1d array
# using list comprehension
flattened_arr = [j for sub in arr_2d for j in sub]
print("Flattened array: ", flattened_arr)


### Output

Original array:  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened array:  [1, 2, 3, 3, 6, 7, 7, 5, 4]


With the help of list comprehension, we have iterated the array and its sub array elements then created the flattened array which is stored in a flattened_arr variable.

### Example

In this example we will using sum() function we will get the 1d array.

arr_2d = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]

# print initial array
print("Original array: ", arr_2d)

# flattening the 2d array into 1d array
# using list sum function
flattened_arr = sum(arr_2d, [])

print("Flattened array: ", flattened_arr)


### Output

Original array:  [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened array:  [1, 2, 3, 3, 6, 7, 7, 5, 4]


The syntax sum(arr_2d, []) flatten the 2D array, here the inbuilt sum() function performs the concatenation of the inner arrays which is like [1, 2] + [3, 4].

Note − This method is not recommended as it takes more time to perform the task.

## Use numpy.flatten() function

We can easily get the flattened array with the help of NumPy flatten() functions. Following is the syntax of this function –

ndarray.flatten(order='C')


The method returns a flattened array from the input N-Dimensional array. Here the parameter order is an optional parameter, and the default value is C.

### Example

In this example we will flatten the 2-dimensional numpy array into a 1-dimensional array using the flattened() function.

import numpy as np
arr_2d = np.array([[1, 2, 3],[4, 5, 6], [7, 8, 9]])
# print initial array
print("Original array: ", arr_2d)
# get the flattened array
flattened_arr = arr_2d.flatten()

print("Flattened array: ", flattened_arr)


### Output

Original array:[[1 2 3]
[4 5 6]
[7 8 9]]
Flattened array:  [1 2 3 4 5 6 7 8 9]


These are the few ways to get the flattened 1D array in python programming.

Updated on: 29-May-2023

55 Views