Array Indexing and Storage

Question

How does Python reference and store array elements?

Answer

First off, we need to distinguish between lists and tuples vs. arrays. In Python, lists and tuples cannot have multiple dimensions, in the sense of an array. You can create lists where each element is another list, and so on, but individual elements in such lists cannot be addressed by traditional "(i,j)" array indexing. To create multidimensional array objects, you'll need to use a module or package like Numeric or array.

Two-dimensional (i.e. rank 2) Python Numeric arrays (which in this discussion is what I'll mean by "array") are indexed "row-column": the first index gives the row number and the second index gives the column number. Thus, array A[i,j] gives the element in the row i and column j of rank 2 array A. Python array indexing is the same as in MATLAB, but different than in IDL (where the first index gives the column number and the second index gives the row number).

However, contiguous Python Numeric arrays are stored in row-major order: if the array has two dimensions, array elements for the first row are stored contiguously, followed by the second row, and so on. IDL array elements are also stored in row-major order, where each row naturally forms an image scanline (because the looping through the first index loops through columns) but in terms of array element storage, Python is different from MATLAB (where elements in the same column are contiguous in memory). As an aside, IDL array indexing and storage both follow FORTRAN convention.

(By the way, in Python, you can have noncontiguous arrays; this regularly can happen when you slice up a large array. Generally you won't have to care whether the array is contiguous or not, as Python will take care of the details of array access for you. If you want to turn a noncontiguous array into a contiguous array, use the Numeric.copy method.)

Numeric array indices are zero-based (like lists and tuples) in Python. Thus, the first element in an array or tuple has an index 0, while the last element in a row of N elements has an index N-1. IDL array indices are also zero-based, while MATLAB indices are one-based, i.e. the first element has index 1.

Confused? :) I sure am :).

(Note the above discussion applies to rank 2 arrays. Higher dimension arrays have an additional quirk, which I'll describe another day when I have time.)

Return to the Tips and Examples index page.

Updated: April 26, 2005 by Johnny Lin <email address>. License.