How to declare an array in Python
In this article, we will learn about arrays and how to declare arrays in Python. We will use some custom codes as well to understand the arrays. Let's first have a quick look over what is an array in Python.
Note: Array does not exist as a built-in data structure in Python. Python uses list type instead of the array.
What is Array
An array is like storage containers where multiple items of the same type are stored. Like other data structures, arrays can also be accessed using indexes. Elements stored in an array have a location as a numerical index. Each index starts from 0 and ends with the length of the array-1. Arrays use contiguous memory locations to store data. Arrays look similar to the Python Lists but have different properties and different declarations.
array1 = [0, 0, 0, 1, 2] array2 = ["cap", "bat", "rat"]
Let us look at the different ways to declare arrays in Python. We will use simple approaches, an array module supported by Python, NumPy module, and also a direct method to initialize an array.
Declare an Array using array module
Array does not exist as a built-in data structure in Python. However, Python provides an
array module to declare a set of data as an array.
arrayName = array(typecode, [Initializers])
typecode - the codes that are used to define the type of value the array will hold.
Initializers - a set of similar type of data
Example: Creating Array using array Module
The below example imports the Python array module. It declares an array of a set of signed integers and prints the elements.
from array import * array1 = array('i', [10,20,30,40,50]) for x in array1: print(x)
Example: Creating array like list in Python
Here, we declare an empty array. Python
for loop and
range() function is used to initialize an array with a default value. You might get confused between lists and arrays but lists are dynamic arrays. Also, arrays stores similar type of data in it while lists store different types of data.
The below example has an empty array. It is initialized with 5 elements carrying a default value (0).
arr =  arr = [0 for i in range(5)] print(arr)
[0, 0, 0, 0, 0]
Example: Python NumPy module to create an array
Python has a module
numpy that can be used to declare an array. It creates arrays and manipulates the data in them efficiently.
numpy.empty() function is used to create an array.
import numpy as np arr = np.empty(10, dtype=object) print(arr)
[None None None None None None None None None None]
Example: Create Array using an initializer
This method creates an array with the default values along with the specified size inside the initializer. See the example below.
arr_num =  * 2 print(arr_num) arr_str = ['P'] * 5 print(arr_str)
['P', 'P', 'P', 'P', 'P']
In this article, we learned to declare an array in Python using various methods such as
array module. We also created empty arrays using
for loop and
range() and discussed a simple approach as well. We also read about the difference between lists and arrays.