In this post, we will learn how to initialize an array in Python like another programming language. Python does not have an array as a built-in datatype instead the lists as an array in python. We often confuse Python list and array. The Python list is a dynamic array and stores elements of different types whereas the array stores a collection of similar datatypes elements in a contagious memory location. The python array module can be used to initialize array operations in Python.
1. initialize an array in python using array module
In this Python program example, We have used an array module to initialize the Python array. The Python array module’s array() method takes two agruments data types and a list of values.
from array import *
array = array('i', [3,6,9,12])
for item in array:
print(item)
output
3
6
9
12
2. initialize empty array in Python
In this Python program example, we will learn how to initialize an empty array in Python that can be done by simply declaring an empty list . Python list is a dynamic array.
#intialize empty array in python
myarr = []
print('python array:',myarr)
Output
python array :[]
3.initialize array with values in Python
In this Python program, we have declared a Python array and initialized it with default values.
#intialize python array with values
myarr = [3,6,9,12,15]
print('initialize python array:\n',myarr)
Output
initialize python array:
[3, 6, 9, 12, 15]
4. Python initialize 2D array
In this Python program example, we have used list comprehension to declare and initialize the python array with default values 0. We will three different ways to initialize the 2D array.
Example 1: initialize python 2D array with 0
array = [[0 for item in range(3)] for i in range(2)]
print(array)
Output
[[0, 0, 0], [0, 0, 0]]
Example 2: initialize 2D python array with 0
rows, cols = (3, 3)
array = [[0]*cols]*rows
print(array)
Output
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Example 3: initialize python 2D array with 0
rows, cols = (3, 3)
array =[]
for x in range(rows):
col = []
for y in range(cols):
col.append(0)
array.append(col)
print(array)
Output
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
5. intialize array in python using * operator
In this Python program example, we have declared Python arrays using the * operator. The multiple operators simply create an array of given default values and multiply as per the given time below examples in the first array value ‘none’ is multiplied 3 times and the second 7 times.
- The first array is initialized with default value none
- The second array is initialized default value on
nparr = [None] * 3
nparr_val = ['on'] * 7
print(nparr)
print(nparr_val)
Output
[None, None, None]
['on', 'on', 'on', 'on', 'on', 'on', 'on']
6. initialize array using for loop and range() function
In this Python program example, we have used for loop and range() function to initialize an array of size 10 with default value 0.
import numpy as np
N_num = 10
init_Val = 0
# intialize array of num element
nparr = [init_Val for item in range(N_num)]
print(nparr)
Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
7.Using NumPy module empty() function
In this Python program, we have used the np. empty(shape) of given shape and specify the datatype ‘object ‘ to create initialized array with ‘none’ values.
import numpy as np
# python intialize array of size n
nparr = np.empty(10, dtype = object)
print(nparr)
Output
[None None None None None None None None None None]
Summary
In this we have learned how to initialize an array in Python with examples initialize an array in python using array module, initialize an empty array in Python, initialize an array with values, Python initialize the 2D array in three different ways, initialize python array using numpy module.