Table of Contents
Creating an Empty Array in Python
Before moving towards various solutions for creating an empty array in Python, we must understand what an empty array means. It means a collection of items with a particular data type containing no items or elements. We can use different ways to create an empty one-dimensional, two-dimensional and multi-dimensional array in Python. Let’s learn each of them one by one.
Use the numpy
Library to Create an Empty Array
Use the numpy
library to create an empty one-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = np.array([]) if array.size == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
First, we imported the numpy
library, an essential Python module for working with arrays. Then, we used the as
keyword to set np
as an alias for the numpy
library. This way, we don’t have to type numpy
again and again; instead, we can use np
.
The numpy module performs various mathematical operations on arrays in Python. By using numpy
, we can add a powerful data structure which ensures efficient calculations with matrices and arrays; it provides an enormous library of complex mathematical functions that operate on these matrices and arrays in Python.
The np.array()
method creates an array with the provided sequence. We used this method to create an empty array. Here, []
denotes an empty list, creating an empty numpy
array. This particular array will have the shape of (0,)
and data type float64
; you can check the shape and data type by using the following code.
1 2 3 4 |
print(array.shape) print(array.dtype) |
1 2 3 4 |
(0,) float64 |
You might be thinking about how the data type is decided to be float64
when an array is empty. Remember, the array’s data type is determined based on the elements of the array; if the array is empty, then its data type is set to float64 by default.
Finally, we used the if-else
block with array.size
to check whether the size of the array
is equal to 0
or not. If it is, print Array is empty
; otherwise, display Array is not empty
. Note that array.size
will return True
if the array is empty; else, False
.
Use the numpy
library to create an empty two-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = np.empty((0,0)) if array.size == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
This code is similar to the previous code example, but we used the np.empty()
method to create a two-dimensional empty array in Python. This method returned a new array of the specified shape, data type and without initializing the entries. In our case, the shape is (0,0)
representing 0
rows and 0
columns while the data type is float64
, which is set by default as we did not specify any data type.
If we want to create an empty 2-D array of int32
data type, we must set the dtype
to int
as follows.
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np array = np.empty((0,0), dtype=int) if array.size == 0: print("Array is empty") else: print("Array is not empty") print(array.shape) print(array.dtype) |
1 2 3 4 5 |
Array is empty (0, 0) int32 |
Use the numpy
library to create an empty multi-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = np.empty((3,4,4)) if array.size == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is not empty |
This example created an array with the shape of (3,4,4)
with the default data type (float64
) and uninitialized data, which means the items in the array
would be some random and meaningless values; this is the reason, the above code resulted in Array is not empty
. You can replace the np.empty()
with the np.zeros()
function to create an array of a particular shape with all items set to 0
.
It does not matter if you replace
np.empty()
with thenp.zeros()
method; the array will not be empty because0
is also a value. This method is useful when you want an array containing a specific shape with all elements set to a particular number.
Further reading:
Use List Comprehension to Create an Empty Array
Use list comprehension to create an empty one-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = [None] * 0 if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
We used list comprehension (a shorter syntax) for this code to create an empty array. We used [None] * 0
to create an empty array; how? The *
was used to repeat an element a given number of times. We repeated the None element 0
times in the above code to create an empty array. Here, the array
would be of list
type.
Next, we used the if-else
block to determine if the specified array is empty; for that, we checked the size of the array
using the len()
function. This function returns the number of elements. So, we used the comparison operator (==
) to check if the returned number of elements is equal to 0
or not. If it is equal to zero, the expression len(array) == 0
will result in a True
and if
block will be executed; otherwise, the else
block will be executed.
Use list comprehension to create an empty two-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = [[0 for i in range(0)] for j in range(0)] if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
Here, we used list comprehension to create a 2-D array in Python. Usually, learners find it hard to understand but believe me, it is easy to understand. First, the outer list comprehension (represented with [... for j in range(0)]
) created a list of sub-lists, whereas the inner list comprehension created every sub-list (represented with [0 for i in range(0)]
).
Here, the range(0)
method returned an empty range, which let the inner list comprehension create an empty array. Finally, the outer list comprehension created a 2-D list with no elements, essentially considered an empty 2-D array.
Use list comprehension to create an empty multi-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = [[[0 for i in range(0)] for j in range(0)] for k in range(0)] if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
This example is the same as we practised earlier than this. The only difference is that we create a multi-dimensional array by adding one more for
in a list comprehension.
The number of dimensions = the number of
for
in a list comprehension
Use the array
Module to Create an Empty Array
Use the array
module to create an empty one-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import array array = array.array("i", []) if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
Here, we used the .array()
method of the array
module to create an empty array. The array module efficiently works with arrays having numeric values. The .array()
method has two arguments: a typecode and an empty list.
The typecode represents the data type of the array’s items. In our case, it is i
, which means integer type. The second argument represented with []
is an empty list which specifies the array will be empty.
Next, we used the if-else
block to print if the array is empty or not based on the specified condition. If you are curious about how this condition worked? [Scroll back](add link using the id
or name
attribute of this paragraph) to learn.
Use the array
module to create an empty two-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = [array.array("i", []) for i in range(0)] if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
Here, we used the .array()
method with a list comprehension. In [array.array("i", []) for i in range(0)]
expression, the .array()
method created an empty array with typecode i
and for
loop specified that the list comprehension must hold no elements.
Use the array
module to create an empty multi-dimensional array in Python.
1 2 3 4 5 6 7 8 9 |
import numpy as np array = [[array.array("i", []) for j in range(0)] for i in range(0)] if len(array) == 0: print("Array is empty") else: print("Array is not empty") |
1 2 3 |
Array is empty |
This example code is similar to the previous one, but we added an extra for
to create an empty multi-dimensional array.
That’s all about how to create empty array in Python.