In this tutorial, you will learn how to create an empty array in Python. Creating an empty array can be useful in various scenarios, such as initializing a container for data to be populated later or creating an array of certain dimensions to manipulate its size and shape.

Python provides multiple ways to create an empty array, including using built-in functions like `list()`

and `numpy`

library, which is a popular library for working with arrays.

### Method 1: Using the List() Function

One of the easiest ways to create an empty array in Python is by using an empty list. An empty list will act as a one-dimensional array. You can create an empty list by using the `list()`

function or by using a pair of square brackets `[]`

.

1 2 |
empty_list = list() empty_list_alt = [] |

### Method 2: Using List Comprehension

List comprehension is a concise way to create lists. You can use list comprehension to create a multi-dimensional empty array. Here’s an example of creating an empty 2-dimensional array of size 3×3:

1 |
empty_2d_array = [[None for _ in range(3)] for _ in range(3)] |

### Method 3: Using the Numpy Library

Although most tasks can be done with Python lists, an array created with the help of the `numpy`

library can be more efficient and useful for numerical operations. If you don’t have the library installed, you can install it using the following command:

1 |
pip install numpy |

To create an empty array using `numpy`

, you can use the `numpy.empty()`

or `numpy.zeros()`

function. The `numpy.empty()`

function returns an array without initializing its elements to any particular values, whereas the `numpy.zeros()`

function creates an array filled with zeros.

Here’s an example of creating a 3×3 empty array using `numpy`

:

1 2 |
import numpy as np empty_numpy_array = np.empty((3, 3)) |

And here’s an example of creating a 3×3 array filled with zeros:

1 2 |
import numpy as np zero_array = np.zeros((3, 3)) |

## Full Code

1 2 3 4 5 6 7 8 9 10 |
import numpy as np empty_list = list() empty_list_alt = [] empty_2d_array = [[None for _ in range(3)] for _ in range(3)] empty_numpy_array = np.empty((3, 3)) zero_array = np.zeros((3, 3)) print(zero_array) |

## Output

[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]

## Conclusion

In this tutorial, you learned about three different methods to create an empty array in Python. Depending on your specific use case, you can use either the `list()`

function, list comprehensions, or the `numpy`

library.

Keep in mind that `numpy`

arrays are useful for numerical operations and can offer better performance compared to Python lists, especially for larger datasets.