When a List is Not Enough- Python Arrays
By: K. Nawab
Tags: Python Programming, Coding, Programming, Python, Arrays

As a beginner in Python, lists have to be my favorite objects to work with. List comprehension reduces multiple lines of code to one line. However, if you have to work with big data, lists may not be the most efficient objects to work with. Therefore we need a more efficient alternative. Say hello to Python arrays.

Arrays behave much like lists but the content is type constrained. You have to specify the content type. Python arrays are much like arrays in C language. Consider using an array specially if your list is going to contain only numbers. When creating an array, you have to provide type code (for a list of type code, I found this page very useful). This is the key to arrays’ efficiency. For example, if you use the type code ‘b’, which in C type is signed char, and can be used for int type in python, each item is stored in a single byte, which for large sequences of numbers, saves a lot of memory. Since arrays are type constrained, Python will not let you put any item that does not fit the type specified. Therefore you are trading off the flexibility of lists but getting efficiency in return. However, most of the things that you can do with lists is supported by arrays as well.

To make an array, you import the array module. The array object takes two arguments, the first one is type and the second is the content. Let’s say we want to make an array holding numbers 0 to 9.

from array import array a = array(‘b’, (x for x in range(9)))

As you can see it is very easy. However to demonstrate the power of arrays, let’s create one with 10 million random floats.

from array import array

from random import random floats = array(‘d’, (random() for i in range(10**7)))

Just like a list, you can check the number of items in an array by using len.

len(floats)

Array also make it very easy to save data into a file and loading it.

with open(‘floats.bin’, ‘wb’) as file:

floats.tofile(file)

To load the file, first be define an empty array and then load the numbers from the saved file into that array.

floats2 = array(‘d’) with open(‘floats.bin’, ‘rb’) as file:

floats2.fromfile(file, 10**7)

Let’s check that the two floats are the same.

floats==floats2

This should return “True”

If you were to save the same amount of data in a text file, it will be much slower and the resulting file will be much larger. Therefore large amount of numeric data can be easily processed and stored as well as loaded efficiently using arrays.

Numpy Arrays

Any article about arrays in python will be incomplete without the mention of Numpy arrays. Numpy arrays are multi-dimensional and can hold not only numbers but user defined objects as well as provide efficient element wise operations.

Lets say we want to define as array that contains numbers from 0 to 12.

import numpy as np a = np.arange(12)

Let’s see what our Numpy array looks like:

a array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

You can check the shape of any array as follow:

a.shape (12, )

We can see that our array has 12 items in one dimension. We can change the shape as follow:

a.shape = 3, 4

Let’s check what our array looks like now:

a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])

To simplify in my head, I think of Numpy arrays as list of lists, but that’s oversimplifying it. However it makes sense when you want to retrieve a row.

a[1] array([ 4, 5, 6, 7])

a[1] thus retrieves the item at the index 1.

If we want to retrieve a specific element, we can use it’s index in both rows and columns.

a[1,2] 5

Think of the first number as the number of the row of the element starting from 0 and the second number is the number of column starting from 0.

We can also pick a whole column. Let’s say I want to pick column 1, with counting starting from 0.

a[:,1] array([ 1, 5, 9])

Last, we can also “flip” the array as follow:

a.transpose() a array([[ 0, 4, 8], [ 1, 5, 9], [ 2, 6, 10], [ 3, 7, 11]])

That’s it! My short introduction to arrays in python.