5 Ways to Initialize a Python Array

Share This Post

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.

5 Ways to Initialize a Python Array


arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

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.


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


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

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

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.


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


a = [0] * 10


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.


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

Read more: How To Fix EOFError


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.

 for i in range(0,10):

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.


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


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

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.


[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?


Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img