Append a NumPy array to an empty array using hstack and vstack

Here we are using the built-in functions of the NumPy library np.hstack and np.vstack. Both are very much similar to each other as they combine NumPy arrays together. The major difference is that np.hstack combines NumPy arrays horizontally and np. vstack combines arrays vertically.

Python3




import numpy as np
  
arr = np.array([])
arr = np.hstack((arr, np.array(['G', 'F', 'G'])))
print(arr)
  
arr = np.vstack((arr, np.array(['G', 'F', 'G'])))
print(arr)


Output:

['G' 'F' 'G'] 

[['G' 'F' 'G'] 
 ['G' 'F' 'G']]


How to append a NumPy array to an empty array in Python

In this article, we will cover how to append a NumPy array to an empty array in Python.  Here, we will discuss 2 different methods to append into an empty NumPy array. Both of these methods differ slightly, as shown below:

Similar Reads

Append a NumPy array to an empty array using the append

Example 1...

Append a NumPy array to an empty array using hstack and vstack

...

Contact Us