89DEVs

Python: How to create an empty array?

How to create an empty array in Python?

To create a new array in Python, use square brackets. If the square brackets are empty an empty array will be created.

Create an empty array

# create an empty array using square brackets myArray = [] print(myArray) In this example, and empty array is by using empty square brackets and assigning it to the variable myArray. Then the empty array is printed. Python returns empty square brackets to indicate that an empty array has been created. [] To check if the created object is a list, use the type() function. The type() function returns the type of the passed element.

Create an empty array of size

To create an empty array of size use asterisk character *. # create an empty array of size myArray = [None] * 3 print(myArray) In this example, an empty array of size 3 is created. The element None is multiplied by 3. This creates an array with 3 None elements. Finally, the array is printed and the result is as shown below. [None, None, None]

Create an empty Array in Numpy

To create an empty Array in Numpy use the full() function. The first argument is the shape of the array and the second argument the fill_value. # create an empty numpy array import numpy as np myArray = np.full([2, 3], None) print(myArray) In this example, numpy is imported as np. Then an array of dimensions 2 x 3 is created. Finally, the array is printed and the result is as shown below. [[None None None] [None None None]]

                
        

Summary


Click to jump to section