Blograby

Working with Multi-Dimension Arrays

Multi-dimension arrays provide all of the same functionality as a single-dimension array. However, as the name implies, a multi-dimension array has more than one dimension to it. For example, here’s a simple multi-dimension array.

[code]MyList = [[1, 2], [3, 4]][/code]

What you get from this code is a 2 × 2 array. To access a particular element, you use its index as you normally would. For example, to access the value 1, you would use:

[code]print MyList[0][0][/code]

You may have already figured out the special feature of IronPython arrays — it’s possible to create ragged arrays without any problem at all. For example, the following code creates a three-element array where the first element is a two-element array.

[code]MyList = [[1, 2], 3, 4][/code]

If you want to access a member of that first element, say 2, you would use the following code.

[code]print MyList[0][1][/code]

Accessing the other elements is straightforward. You access them as you would a single-dimensional array. For example, to access the number 3, you use the following code.

[code]print MyList[2][/code]

In all other respects, IronPython multi-dimension arrays work like their single-dimension counterparts. You can perform an amazing number of tasks using multi-dimension arrays.

 

Exit mobile version