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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 17 May

    Add Month to datetime in Python

    Table of ContentsWays to add month to datetime in PythonUsing the datetime.datetime constructor to add month to datetime in Python.Using the dateutil.relativedelta object to add month to datetime in PythonUsing the pandas.DateOffset() function to add month to datetime in PythonUsing the arrow.shift() function to add month to datetime in PythonConclusionWas this post helpful? In Python, […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusionWas this post helpful? In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusionWas this post helpful? In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusionWas this post helpful? What are the types of arrays? An array is a common structure […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.