Arrays in Python
Written by Alex Armstrong   
Monday, 19 March 2012
Article Index
Arrays in Python
Dimensions and Comprehensions

One of the most fundamental data structures in any language is the array. Python doesn't have a native array data structure, but it has the list which is much more general and can be used as a multidimensional array quite easily.

If you are working with NumPy then read:
     Advanced Python Arrays - Introducing NumPy.

 

Well perhaps "easy" is only applicable after you have mastered how to translate what you already know from other languages into list constructs. In this short article the objective is to spell out and make perfectly clear how you can use a list as an array and how to think about the process in terms that will be familiar to programmers who just want to know how to implement something they know well but in Python.

At this point is it worth mentioning the extensive array handling operations and objects in the NumPy library. If you have a lot of numeric arrays you want to work with then it is worth using the library. However, this article is about implementing array operations using the basics provided by Python. Even if you do go on to use NumPy, it is worth knowing how to do it without.

List basics

A list in Python is just an ordered collection of items which can be of any type. By comparison an array is an ordered collection of items of a single type - so in principle a list is more flexible than an array but it is this flexibility that makes things slightly harder when you want to work with a regular structure. A list is also a dynamic mutable type and this means you can add and delete elements from the list at any time.

To define a list you simply write a comma separated list of items in square brackets:

myList=[1,2,3,4,5,6]

This looks like an array because you can use "slicing" notation to pick out an individual element - indexes start from 0. For example

print myList[2]

will display the third element, i.e. the value 3 in this case. Similarly to change the third element you can assign directly to it:

myList[2]=100

The slicing notation looks like array indexing but it is a lot more flexible. For example

myList[2:5]

is a sublist from the third element to the fifth i.e. from myList[2] to myList[4]. notice that the final element specified i.e. [5] is not included in the slice.

Also notice that you can leave out either of the start and end indexes and they will be assumed to have their maximum possible value. For example

myList[5:]

is the list from List[5] to the end of the list and

myList[:5]

is the list up to and not including myList[5] and

myList[:]

is the entire list.

List slicing is more or less the same as string slicing except that you can modify a slice. For example:

myList[0:2]=[0,1]

has the same effect as

myList[0]=0
myList[1]=1

Finally is it worth knowing that the list you assign to a slice doesn't have to be the same size as the slice - it simply replaces it even if it is a different size.

Basic array operations

So far so good, and it looks as if using a list is as easy as using an array. The first thing that we tend to need to do is to scan through an array and examine values. For example, to find the maximum value (forgetting for a moment that there is a built-in max function)  you could use:

m=0
for e in myList:
if m<e:
m=e

This uses the for..in construct to scan through each item in the list. This is a very useful way to access the elements of an array but it isn't the one that most programmers will be familiar with. In most cases arrays are accessed by index and you can do this in Python:

m=0
for i in range(len(myList)):
if m<myList[i]:
m=myList[i]

Notice that we are now using range to generate the sequence 0,1, and so on up to the length of myList.

You have to admit that there is a lot to be said for the simplicity of the non-index version of the for loop but in many cases the index of the entry is needed. There is the option of using the index method to discover the index of an element but this has its problems.

For example if you wanted to return not the maximum element but its index position in the list you could use:

m=0
mi=0
for i in range(len(myList)):
if m<myList[i]:
m=myList[i]
mi=i

or you could use the non-indexed loop and the index method:

m=0
for e in myList:
if m<e:
m=e
mi=myList.index(m)
print mi

The only problem is that index(m) returns the index of m in the list or an error if m isn't in the list. There is also the slight problem that behind the scenes it is clear that index is performing another scan of the entire list.

<ASIN:1871962587>

<ASIN:1439896941>

<ASIN:0596158068>

<ASIN:1449382673>



Last Updated ( Sunday, 29 July 2018 )