Python Arrays

Python does not have built-in support for arrays like other programming languages like C, C++ etc. But it has many data types like lists that can be used as arrays. Except for one thing, in a typical array, all elements should be of the same type, but in lists, this is not mandatory.

What is an array?

An array is like a container that can have multiple elements. Each item stored is called an element and they are stored at adjacent memory locations. Each element is identified by a numerical value called an index. Index starts from 0 and increments by one. That means if an array has four elements, the first element would be at index 0, the second element would be at index 1 and so on.

Creating Arrays in Python

By using lists

Cities = [“Mumbai”, “Delhi”, “Kolkata”, “Chennai”]

The above is a list containing four string values. A list in Python works exactly like an array except, that it can have values of multiple types.

For example

myList = [“Mumbai”, 1.5, 7, False]

This list contains a string, a float, an integer, and a boolean value. which is of course not possible when working with a typical array.

By using Python Built-In array module

To create and use an array in Python, we can import the built-in array module and use its array function. We can create an array of three basic types – integer, float, and unicode (string).

The array function accepts two arguments – typecode, and the initializers.

Syntax

Import array as name_of_array
any_name = name_of_array.array(typecode, [initializers])

Example

Import array as arr
my_array = arr.array(“i”, [1,7,4,10,14])

Accessing array elements

To access any element in an array, we use its index. As explained above, the index always starts from 0 and increments by one.

Example

Import array as arr
my_array = arr.array(“i”, [1,7,4,10,14])
print(my_array[0])

Output:

1

Note: The largest index value of an array is always one less than the number of elements. This is because the index starts from 0. So if an array contains two elements, its indexes would be 0, and 1 (This is the largest index which is one less than the number of elements, i.e., 2).

Changing elements in an array

Like Python lists, arrays are mutable, that means we can change the elements in an array.

Example

myArr = [1,4,5,7]
myArr[1] = 10
print(myArr)

Output

[1,10,5,7]

Deleting elements from an array

We can delete an element from an array using Python’s built-in del statement. To delete an element we use the index of that element.

Example

myArr = [1,4,5,7]
Del myArr[0]
print(myArr)

Output

[4,5,7]

Finding the length of an array

We can find the length (number of elements) of an array using Python’s built-in len function.

Example

myArr = [1,4,5,7]
arrLength = len(myArr)
print(arrLength)

Output

4

Array concatenation

Array Concatenation means joining arrays together. We can join arrays in Python using the ‘+’ operator.

Example

arr1 = [1,4,5,6]
arr2 = [2.4, 4.5, 2.1, 8.7]
arr3 = arr1 + arr2
print(arr3)

Output

[1,4,5,6,2.4, 4.5, 2.1, 8.7]

Looping through array elements

myArr = [1,4,5,7]

for x in myArr:
    print(x)

Output

1
4
5
7