Skip to content

5 Ways to Initialize a Python Array

  • by
Initialize a Python Array

An array, in the English language, refers to an ordered series of arrangements. In a programming language, an array can be defined as a collection of items stored at contiguous memory locations. let begin! and learn 5 Ways to Initialize a Python Array

Python does not have built-in support for arrays (we can import them from the “array” module) but lists can use instead. In this article, we will learn how we can initialize an empty array of a given size.

Contents

5 Ways to Initialize a Python Array

METHOD 1:


arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print(arr)

Explanation: This is the most basic method of filling an empty list since everything is done manually.

All we had to do is create a list, give it a name (arr in our case), and fill it with our desired values.

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

METHOD 2

In this method, we will initialize a list using loops.


a = []
a = [0 for x in range(10)]
print(a)

Explanation: In the first step we simply declared an empty list. Next, we iterated a for-loop within a given range, which in our case is 10, and stored the value 0 inside the empty list.

Then we used the print statement by which we can prompt all elements of our newly filled list on the terminal.

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

METHOD 3


a = [0] * 10

print(a)

Explanation: This method is quite similar to the one explained above since they only differ in syntax. All we had to do was create a variable for a list, “a” in our case, and assign the value.

We want the list to store. In this example, the program will store the value 0 in our list 10 times. It is as simple as that.

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Read more: How To Fix EOFError

METHOD 4

Here we will make use of a property called append(). The append() method in python adds a single item in a list that has already been defined. It does not create a new list rather it modifies the existing list by adding the item at the end of the list.


arr=[]
 for i in range(0,10):
 	arr.append(0)
 	print(arr)

Explanation: First we created an empty list and named it “arr”. Next, we used a for-loop to iterate through the range, which is 10 in our case, and with each iteration, we told the program to add a 0 at the end of the list using the append() method. The resultant list will have 10 places, all filled with a 0.

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

METHOD 5



import numpy
n = 10
arr = numpy.empty(n, dtype = object)
print(arr)

Explanation: This method makes use of the empty() method of the NumPy module of python.

The empty() method is used to create a new array of given shapes and types, without initializing entries. The default value of the items in a list created by using the empty() method will be None.

Output:

[None None None None None None None None None None]

This is about everything you need to know about initializing an array in python.

Read more: How to fix Pygame. error: video system not initialized?

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *