Initialize an empty array in Python

In this Python tutorial, We will discuss how built-in support works for the array in Python.

There are multiple ways to create an empty array in Python, which are shown below:

  • Using the square brackets
  • Using the array module
  • Using the NumPy module
  • Using the collections module
  • Using the empty method
  •  Using the zeros method
  • Using the full method
  • Using the arange() method

Create empty array Python

Here we will see how to create an empty array in Python using a variety of different methods. Let us start with the first one.

Method-1: Create empty array Python using the square brackets

The simplest way to create an empty array in Python is to define an empty list using square brackets.

empty_array = []

The above code creates an empty list object called empty_array. This list can be used to store elements and perform operations on them.

Read: Python program to print element in an array

Method-2: Create empty array Python using the array module

The array module provides a way to create arrays with a specific data type. To create an empty array with the array module, you can call the array function and pass an empty list as the data argument.

# Importing the array module
import array

# Creating an empty array with data type 'i' (signed integer)
empty_array = array.array('i')

The above code imports the array module and creates an empty integer array, named empty_array, using the array constructor. The ‘i’ argument specifies that the elements of the array should be integers.

READ:  How to Create a String with Variables in Python [Create a String with Multiple Variables in Python]

Read: Python program to get shape of an array

Method-3: Create empty array Python using the NumPy module

The numpy module is a powerful library for working with arrays in Python. To create an empty array with the numpy module, you can call various methods, such as empty, zeros, or ones, and pass a shape argument that specifies the dimensions of the array.

# Import numpy library
import numpy as np

# Creating an empty numpy array
empty_array = np.array([])

The above code imports the numpy library as np and creates an empty numpy array with the np.array function and an empty list as an argument. The empty numpy array is stored in the empty_array variable.

Method-4: Create empty array Python using the collections module

The collections module provides a way to create arrays with a specific data type, similar to the array module. To create an empty array with the collections module, you can call the array function and pass an empty list as the data argument.

# Importing the 'array' module from the 'collections' library
from collections import array

# Creating an empty array with type code 'i' for signed integers
empty_array = array('i')

The above code is written in Python and it creates an empty array using the array module from collections. The array is created as an array of signed integers (‘i’) using the array constructor. The variable empty_array will store the reference to the newly created array.

Read: Python concatenate arrays

Method-5: Create empty array Python using the empty method

The empty method from the numpy module creates an array with uninitialized data. To create an empty array with this method, you can call the empty method and pass a shape argument that specifies the dimensions of the array.

# Import numpy module
import numpy as np

# Create an empty numpy array with size 0
empty_array = np.empty(0)

The above code imports the NumPy library as np and creates an empty array using the empty method from the numpy library.

READ:  How to Check if a Python Set is Empty? [6 Ways]

The method takes in a single argument 0 which is the shape of the array. The empty array created will have a shape of (0,), meaning it has 0 rows and 1 column.

Method-6: Create empty array Python using the zeros method

The zeros method from the numpy module creates an array filled with zeros. To create an empty array with this method, you can call the zeros method and pass a shape argument that specifies the dimensions of the array.

# Importing the numpy module
import numpy as np

# Creating an empty array using np.zeros
empty_array = np.zeros(0)

The above code creates an empty numpy array with data type float (default data type in numpy) using the np. zeros function from the numpy library.

  • The np.zeros function creates a new array of given shape and type, filled with zeros. In this case, the shape of the array is 0 which means an empty array.

Read: Python convert dictionary to an array

Method-7: Create empty array Python using the full method

The full method from the numpy module creates an array filled with a specified value. To create an empty array with this method, you can call the full method and pass a shape argument that specifies the dimensions of the array, as well as the value to fill the array with.

import numpy as np  # Import the numpy library

# Create an empty numpy array with zero elements, initialized to None
empty_array = np.full(0, None) 

In the above code, the numpy library is being imported and an empty numpy array is being created with zero elements, all initialized to None.

READ:  Python List pop() method [With Examples]

Read: How to Convert Python string to byte array with Examples

Method-8: Create empty array Python using the arange() method

To create an empty NumPy array using the arange() method, you can set the start and end values to the same value.

import numpy as np

# Define the size of the array
n = 10

# Define the starting and ending points of the array
start = end = 0

# Create an array of integers of length `n` with values ranging from `start` to `end`
my_array = np.arange(start, end, dtype=np.int32)

The code above uses the NumPy library to create an array of integers. The n variable determines the size of the array, while the start and end variables determine the range of values that will be included in the array.

Create empty array Python
Creating an empty array in Python

You may also like to read the following Python tutorials.

In this tutorial, we learned how to create an empty array in python by using the following methods:

  • Using the square brackets
  • Using the array module
  • Using the NumPy module
  • Using the collections module
  • Using the empty method
  •  Using the zeros method
  • Using the full method
  • Using the arange() method