In this NumPy article, I will explain how NumPy create an empty array in Python using the np.empty() function. Also, we will see how to create an empty NumPy array of strings in Python.
To create an empty array in Python, we can use the np.empty() function from the NumPy library. The empty function in Python NumPy, takes a shape argument, which is a tuple of integers indicating the array’s dimensions, and a dtype argument, which can help to create an empty array of strings.
NumPy create an empty array in Python
One of the basic operations in NumPy is creating an empty array, which can be useful as a placeholder for data that will be added later through Python.
We can use the np.empty in NumPy Python library function to create an empty array in Python.
NumPy create empty array in Python using np.empty()
The np.empty() is a function in the NumPy library of Python, which is used to create a new array of a specified shape and type, without initializing entries.
Syntax and Parameters:
numpy.empty(shape, dtype=float, order='C')
Here,
Name | Description |
---|---|
shape | This parameter specifies the shape of the empty array in Python. It can be an integer or a tuple of integers indicating the array’s dimensions. |
dtype | (optional) It defines the data type of the array elements in Python. The default is float. |
order | (optional) It can be either ‘C‘ for C-style row-major order or ‘F‘ for Fortran-style column-major order. The default is ‘C‘. |
Let’s see some examples to understand how NumPy create an empty array in Python using the NumPy empty function in Python.
How to initialize a NumPy empty array
The basic usage of np.empty() involves specifying the shape of the array we want to create in Python. The shape is a tuple that indicates the size of each dimension of the Python array.
Here’s a simple example:
import numpy as np
array = np.empty((2, 3))
print(array)
Output: The implementation of the code is given below:
[[0. 0. 0.]
[0. 0. 0.]]
This way, NumPy create an empty array in Python using the np.empty() function.
NumPy empty string array
To create an empty string array using the np.empty() in Python, we need to specify the dtype parameter as str or object.
However, remember that np.empty() does not actually create “empty” strings; it just allocates memory space for the strings. Here’s how we can do it:
import numpy as np
string_array = np.empty((2, 2), dtype=str)
print(string_array)
Output: After implementation, the output of the Python code is given below:
[['' '']
['' '']]
This way NumPy create an empty array in Python.
Create an empty NumPy array without shape
To create an empty NumPy array in Python without explicitly defining its shape is not straightforward, as np.empty() requires a shape argument.
However, one could technically create an empty array by specifying a shape with zero in one or more dimensions:
import numpy as np
empty_array = np.empty((0, 3))
print(empty_array)
Output: The implementation of the code is mentioned below:
[]
This way we can use the shape parameter in the empty() function, where NumPy create an empty array in Python.
Conclusion
This tutorial covers the essentials of how NumPy creates an empty array in Python, along with an alternative method for array initialization and important considerations i.e., np.empty() function with different parameters like dtype, shape, and order.
Also, how different parameters of the empty() function can help us to create various empty arrays in Python.
You may also like to read:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.