[Tutor] I want some help with arrays...

Alan Gauld alan.gauld at btinternet.com
Fri May 18 00:48:01 CEST 2007

"Alan Gilfoy" <agilfoy at frontiernet.net> wrote
>>>> array = [["1.1", "1.2"], ["2.1", "2.2"]]
>>>> array[1[2]]

This is trying to get the second element of 1.
But one doesn't have a second element, hence the error.

Just add an extra bracket:


But that won;t work either because the index starts at 0 so it needs 
to be:


> When I nest the slices ( array[1[2]] ) I get that error message.

Note that this is not slicing. Slicing is extracting a subsection
of an array (or list) using the end points as indices

lst = [1,3,5,7,9]
slice = lst[2:4]

will put the 3rd and 4th elements into slice(zero indexing)

Thats quite different to indexing to select a single value.

Finally, you can use layout when defining your multi
dimensional data to make it more obvious:

table = [ [1,2,3],
             [7,8,9] ]

print table[1][1]     #---> 5

You will get some more on data types including arrays and other
classic data structures in my tutorial under the Raw Materials topic.
Look for the Collections heading about 40% through and it extends
to the Files heading about 80% through...

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list