How to initialize array in Python

In this article, we will see a different ways to initialize an array in Python.

Array is collection of elements of same type. In Python, we can use Python list to represent an array.

Using for loop, range() function and append() method of list

Let’s see different ways to initiaze arrays

Intialize empty array

You can use square brackets [] to create empty array.

Empty array: []

Intialize array with default values

Here, we are adding 0 as a default value into the list for n number of times using append() method of list. for looping each time we are using for loop with range() function.

Below is the Python code given:

Here range() function is used to generate sequence of numbers. We are using range(stop) to generate list of numbers from 0 to stop.
Output:

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

Intialize array with values

You can include elements separated by comma in square brackets [] to initialize array with values.

[10, 20, 30, 40]

Using list-comprehension

Here, list-comprehension is used to create a new list of n size with 0 as a default value.

Below is the Python code given:

Output:

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

Using product (*) operator

Here, product operator (*) is used to create a list of n size with 0 as a default value.

Below is the Python code given:

Output:

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

Using empty() method of numpy module

here, empty() method of numpy is used to create a numpy array of given size with default value None.

Below is the Python code given:

Output:

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

That’s all about how to initialize array in Python.

Related Posts

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

  • 06 April

    Print Matrix in Python

    In this article you will learn "How to print a matrix in Python?"

  • 18 November

    Print Array in Python

    Table of ContentsPrint ListUsing print()Using map()By unpacking listUsing loopPrint Numpy-ArrayUsing print()Using loop In this post, we will see how to print array in Python. As we know that, Python didn’t have an in-built array data type, so we try to use list data type as an array. We can also use the NumPy module for […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.