Initialize A Python Array In 3 Ways

Throughout this article, we will be looking at some Simple Methods To Initialize A Python Array.

The Python array data structure is a data structure that stores data values that are similar across contiguous memory locations. When compared to a List(dynamic Arrays), Python Arrays stores elements of the same type in a more compact manner.

A Python List, on the other hand, can contain elements of different data types. Now, let’s take a look at the various methods for initializing an array in the Python programming language.

1. Direct Method To Initialize A Python Array

The following command can be used to initialize the data values of the array while it is being declared.

Syntax:

Syntax:

nameOfArray = [Default Value]*size

Where,
     Default Value = It is optional Parameter and If you do not want size then you can keep it empty.
     size = It is also an Optional Parameter, used when you want to specify a size of array during initialization.
#Initializing the Python Array Directly
newArray = [1]*5

newArrayChar = ['Code']*3

#Printing the Above Python Array
print(newArray)
print(newArrayChar)

Output:

[1, 1, 1, 1, 1]
['Code', 'Code', 'Code']

According to the preceding example, we have created two arrays with default values of 1 and ‘Code’, respectively, as well as the specified size for each of them.

2. Using List Comprehension Method

To initialize an array with a default value, the for loop and range() functions in Python can be used in conjunction with one another.

With a single argument, the range() function in Python returns a sequence of numbers that begins with 0 and ends with the specified number, each of which is increased by one.

The for loop in Python would assign the value 0(default-value) to every element in the array that fell within the range specified by the range() function.

Syntax:

nameOfArray = [n for i in range(x)]

where,
      n = can be any number you want as default value.
      x = is the size of the array you want.
#Initializing the Empty Python Array
newArray = []

#Using the List Comprehension Method
newArray = [5 for i in range(5)]

#Printing the Above Python Array
print(newArray)

As you can see in the code above, I first created an empty array in Python and then used the list comprehension method to set the array’s default value and size.

3. Using NumPy Array To Initialize A Python Array

The NumPy module in Python can be used to efficiently create arrays and manipulate the data contained within them. The numpy.empty() function creates an array of a specified size with a default value of ‘None’, which is the default value for the array.

Syntax:

nameOfArray = numpy.empty(size,dtype=object)

where,
    size = Size of the Array
    dtype = Data Type Of the Elements in Array
#Importing NumPy
import numpy as np

#Initializing A Python Array
newArray = np.empty(5, dtype=object)

#Printing The Array Initialized Above
print(newArray)

Output:]

[None None None None None]
Initialize A Python Array In 3 Ways

Wrap Up

I hope you have gained an understanding of the three different ways you can initialize a Python array. Although the first method discussed above is the most recommended and is considered best practice, there are other methods that can be used.

As a result, we have come to a conclusion on this topic. Please feel free to leave a comment below if you have any questions or concerns.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Reverse an Array in Python
  2. How to Print an Array in Python
  3. Remove a Character From String In Python
  4. How To Append Or Add String In Python

Leave a Comment