# Python – Initialize empty array of given length

Last Updated : 18 Mar, 2024

As we know Array is a collection of items stored at contiguous memory locations. In Python, a List (Dynamic Array) can be treated as an Array. In this article, we will learn how to initialize an empty array of some given size. Let’s see different Pythonic ways to create an empty list in Python with a certain size.

## Python – Initialize an empty array of a given length

Below, are the methods to Initialize an empty array of a given length in Python.

• Using * Operator
• Using List Comprehension
• Using For Loop
• Using NumPy
• Using repeat() Method

### Method 1: Initialize empty array using * Operator

In this example, we are creating different types of empty using an asterisk (*) operator.

Python3 ```# initializes all the 10 spaces with 0â€™s a = [0] * 10 print("Intitialising empty list with zeros: ", a) # initializes all the 10 spaces with None b = [None] * 10 print("Intitialising empty list of None: ", b) # initializes a 4 by 3 array matrix all with 0's c = [[0] * 4] * 3 print("Intitialising 2D empty list of zeros: ", c) # empty list which is not null, it's just empty. d = [] print("Intitialising empty list of zeros: ", d) ```

Output
```Creating empty list contains zeros:  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Creating empty list of None:  [None, None, None, None, None, None, None, None, None, None]
Creating 2D empty list of zeros:  [[0, 0,...

```

### Method 2: Create an empty list in python with a certain size using list comprehension

In this example, we are using Python List comprehension for 1D and 2D empty arrays.

Python3 ```# initialize the spaces with 0â€™s with # the help of list comprehensions a = [0 for x in range(10)] print(a) b = [[0] * 4 for i in range(3)] print(b) ```

Output
```[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

```

### Method 3: Create an empty list of a specific size in Python using a loop

In this example, we are using a Python loop for 1D and 2D empty arrays.

Python3 ```b= [] for x in range(5): b.append([[]]) print(b) c = [] for x in range(5): c.append(0) print(c) ```

Output
```[[[]], [[]], [[]], [[]], [[]]]
[0, 0, 0, 0, 0]

```

### Method 4: Initialize empty array using Numpy

In this method, we are using the Numpy module to generate an empty matrix of 1D and 2D sizes using np.empty().

Python3 ```import numpy # create a simple array with numpy empty() a = numpy.empty(5, dtype=object) print(a) # create multi-dim array by providing shape matrix = numpy.empty(shape=(2, 5), dtype='object') print(matrix) ```

Output
```[None None None None None]
[[None None None None None]
[None None None None None]]

```

### Method 5: Initialize empty array using repeat() Method

In this method, we can use the repeat() function from the itertools module to create a new iterator that returns the same value a certain number of times. This can be used to create an empty array of a specific size by providing a value of None and the desired length.

Python3 ```import itertools #Initialize empty array with length 10 filled with 0's a = list(itertools.repeat(0, 10)) print(a) #Initialize 2D empty array with 3 rows and 4 columns filled with 0's b = [list(itertools.repeat(0, 4)) for i in range(3)] print(b) ```

Output
```[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

```

This method has a time complexity of O(n) and an auxiliary space of O(n) where n is the desired length of the array.

Reference : List in Python

Previous
Next