How to Initialize an Empty Array of given Length using Python


An empty array consists of either a null value or no element. The empty array is very special to programming when the programmer uses the condition to perform specific tasks. In Python, we have some built−in functions such as empty(), append(), range(), and, extend() will be used to Initialize an empty array of the given length.

Syntax

The following syntax is used in the examples

empty()

The empty() is an in−built function in Python that returns the new array in the form of given length and type.

append()

The append() is an in−built function in Python that inserts the element at the end of the given list.

range()

The built−in function range() can be used to generate the order of numbers.

extend()

The built−in function extend() insert the specified element at the end of the current list.

Using Multiplication(*) operator

In Python, the multiplication operator will help concate two different values and initialize an empty array of the given length.

Example

In the following example, start the program by setting the length value in the variable l. Then use the multiplication operator between ‘[none]’ and ‘l’ to create the empty array of a given length in the variable arr. This process will generate the empty array.

l = 5
arr = [None] * l
print("The empty array of a given length:\n", arr)

Output

The empty array of a given length:
[None, None, None, None, None]

Using empty() Function

The program uses built−in method empty() that follow the numpy module to initialize the empty array with given length.

Example

In the following example, we will start the program by defining the module named numpy and taking the object reference as np. Then use the built−in function empty that accepts two parameters− 5(set the range length) and dtype(set the type of data). This function is stored in the variable x and using the same display the result.

import numpy as np
x = np.empty(5, dtype = object)
print("The empty array of a given length:\n", x)

Output

The empty array of a given length: 
[None None None None None]

Using range() and append() Function

The program uses a built−in function range that returns a series of numbers to iterate into the loop. Using append it will add the item to the current list.

Example

In the following example, we will use the for loop where variable i iterate through the range of the given length, and using append() it inserts the value called none to each index.

l = 5
arr = []
for i in range(l):
    arr.append(None)
print(arr)

Output

 [None, None, None, None, None]

Using while loop and extend() Function

The program uses a while loop to iterate through each index and using extend() it inserts the array value according to the parameter.

Example

In the following example, we will start the program by initializing the empty list in the variable emp_arr to store the final result. Then set the value i which represents the initial value for the iteration and using the while loop and built−in function extend() it iterates to insert the list to a particular index. Next, print the result with the help of variable emp_arr.

emp_arr = []
i = 0
while i < 10:
    emp_arr.extend([[]])
    i += 1
print("The empty array according to given length:\n", emp_arr)

Output

The empty array according to a given length:
 [[], [], [], [], [], [], [], [], [], []]

Conclusion

We discussed the various built−in function related to empty arrays and it is possible to solve the problem statement. The above program represents the loop iteration using some built−in functions such as extend(), append(), and, range(). The empty array is required when we add something to the array whether it's null or any data values.

Updated on: 14-Aug-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements