Python Array


Python Array

In this tutorial, you will learn about array built-in module. The array module is used to represent an array of characters, integers, and floating point numbers.

Unlike Python lists, the Python arrays are efficient with numeric values.

Difference between a Python Array and a Python List

The basic difference between a Python list and a Python array is that, an array can store values of a specified datatype, whereas list can store values of any datatype.

For example, in the following code snippet, my_array is a Python array created using Python array module, and my_list is a Python list.

# Array
import array as arr
my_array = arr.array('i', [2, 4, 6, 8])

# List
my_list = [2, 'apple', 3.14, 58, "hello"]

my_array is initialized with integer type values, and it can store only integer values.

my_list is initialized with integer, string, and floating point number values, and it can store elements of any datatype.

So, you may be wondering why use an array, when we have lists. Because, lists are built for versatility, while arrays are built for performance. Therefore, if your application does lots of operations on arrays, and you want your application to be efficient, using arrays can be the first big step you can take in making your application fast.

Create Python Array

To create a Python array, we need to import array module. We shall import the array module as arr, as shown in the following statement.

import array as arr

array() method of array module can be used to create an Array. This method takes two arguments.

  • The first argument specifies the type of the values that we store in the array.
  • The second argument is a list of the values using which the array is initialized.

We specify the type of values in the array, via first argument, using a Type Code. The following table specifies the type code, respective C type (datatype in C language), respective datatype in Python, and the minimum number of bytes allocated to each of the item in the array.

Type CodeC data typePython data typeMinimum number of bytes
'b'signed charint1
'B'unsigned charint1
'u'Py_UNICODEUnicode2
'h'signed shortint2
'H'unsigned shortint2
'i'signed intint2
'I'unsigned intint2
'l'signed longint4
'L'unsigned longint4
'f'floatfloat4
'd'doublefloat8

For example, to create an array of signed integers, we have to specify the type code 'i' as first argument to the array() method.

Create Singed Integer Array

Now, let us create an array of singed integer array. As already discussed, pass 'i' as the first argument, and the list of initial values in the array as second argument.

Python Program

import array as arr

my_array = arr.array('i', [2, 4, 6, 8, 10])

print(my_array)

Output

array('i', [2, 4, 6, 8, 10])

Create Array with Floating Point Numbers

Similarly, let us create an array of floating point numbers. We have to pass 'f' as first argument to the array() method.

Python Program

import array as arr

my_array = arr.array('f', [1.5, 2.8, 3.14, 6.2])

print(my_array)

Output

array('f', [1.5, 2.799999952316284, 3.140000104904175, 6.199999809265137])

Create Empty Integer Array

We can create an empty array with no elements in it, just specifying the type of elements in the array. For that, do not pass the second argument, or pass an empty list.

In the following program, we create an empty integer array my_array.

Python Program

import array as arr

my_array = arr.array('i')

print(my_array)

Output

array('i')

Length of Python Array

We can get the length of given Python Array using len() builtin function.

In the following program, we take an integer array my_array, and find its length using len().

Python Program

import array as arr

my_array = arr.array('i', [200, 400, 600, 800])

print("Size of array : ", len(my_array))

Output

Size of array :  4

Access Elements of Python Array

We can access the elements of a given Python array using index.

The syntax to access the element at index=i in the array my_array is

my_array[i]

You may read the value returned by the above expression, or you can assign a new value to that element.

Read elements of Python array

Let us first read the elements in the array using index.

In the following program, we initialize an integer array my_array, and then print its elements using index.

Python Program

import array as arr

my_array = arr.array('i', [200, 400, 600, 800, 900])

print("Element at index=0 is : ", my_array[0])
print("Element at index=1 is : ", my_array[1])
print("Element at index=2 is : ", my_array[2])

Output

Element at index=0 is :  200
Element at index=1 is :  400
Element at index=2 is :  600

Update elements of Python array

We can assign new values to the elements of Python array using index.

In the following program, we initialize an integer array my_array, and then update the element at index=3 with a new value of 199.

Python Program

import array as arr

my_array = arr.array('i', [200, 400, 600, 800, 900])
my_array[3] = 199

print(my_array)

Output

array('i', [200, 400, 600, 199, 900])

Appending Elements to Python Array

We can append new elements to given Python array using append() method. Call append() method on the Python array object, and pass the element as argument.

The syntax to append an element value to the array my_array is

my_array.append(value)

In the following program, we initialize an array with four elements. Then we shall use append() method to append an element 1000 to the array.

Python Program

import array as arr

my_array = arr.array('i', [200, 400, 600, 800])
my_array.append(1000)

print(my_array)

Output

array('i', [200, 400, 600, 800, 1000])

Python Arrays Tutorials

The following list of tutorials provide a detailed description and examples for each of the topics related to Python Arrays.

Summary

In this tutorial, we learned about array module in Python, how to use this module to create arrays of integers, characters, or floating point numbers, and different actions that we can perform on these arrays, with examples.