Understanding Tuples, Lists, and Arrays

0
137

As with many languages, IronPython has its own set of odd terminology. Any time you see a list of items separated by commas such as this:

[code]MyTuple = ‘Hello’, ‘Goodbye’, ‘Red’, ‘Green’[/code]

the precise terminology for the structure is a tuple. IronPython also supports lists natively. Listing 4-1 shows a simple application that creates a list and then displays its content. Figure 4-1 shows the output from this example.

Listin g 4-1: Creating and using a list

[code]# Define a list
MyList = [‘Hello’, ‘Goodbye’, ‘Red’, ‘Green’]
# Display each list element.
for TheString in MyList:
print TheString
# Pause after the debug session.
raw_input(‘Press any key to continue…’)[/code]

Lists produce the same output as a tuple given the same input.

The main difference between a tuple and a list is that you can change the content of a list, but you can’t change the content of a tuple. If you create a tuple and try to change one of its elements, you get an error message. Listing 4-2 shows what happens when you try to change a tuple and a list.

Listin g 4-2: Changing tuples and lists

[code]# Create the tuple
MyTuple = ‘Red’, ‘Green’, ‘Blue’, ‘Yellow’
# Create the list
MyList = [‘Red’, ‘Green’, ‘Blue’, ‘Yellow’]
# Attempt to change the tuple, which will result in an error.
try:
MyTuple[1] = ‘Orange’
except TypeError:
print ‘Couldn‘t change the tuple.n’
# Attempt to change the list.
try:
MyList[1] = ‘Orange’
except TypeError:
print ‘Couldn‘t change the list.n’
# Verify that the change worked.
print ‘Displaying the list content.’
for TheString in MyList:
print TheString
# Pause after the debug session.
raw_input(‘Press any key to continue…’)[/code]

The example attempts to change both a tuple and a list. However, when the example tries to change the tuple, it generates an exception, as shown in Figure 4-2. Notice how the code uses a try…except structure to catch the exception and display an error message onscreen. The except clause can stand alone, but it’s better to provide a particular kind of exception, which is TypeError in this case. The code does successfully modify the list and displays the results in Figure 4-2.

Lists are mutable; tuples aren’t.

Tuples and lists are more similar than different. For example, it’s possible to add elements to a tuple, just as you can to a list. In fact, except for the fact that tuples are immutable (not changeable) and lists are mutable (you can change them), both structures provide precisely the same features. Both of these structures are also roughly equivalent to arrays in other languages and you can call them arrays without provoking too many odd reactions — at least, not from normal people.

Of course, you’re wondering now why Python and IronPython support two array-like structures. Tuples come in handy for a few reasons. First, because tuples can’t change, IronPython can implement them more efficiently, which means that using tuples is faster. Second, tuples are less susceptible to outside influences such as viruses. Because you can’t change the content of a tuple, you also can’t fill it with things that the application can’t use. Third, tuples are marginally faster to type.

The object that IronPython uses as an array really isn’t an array in the common sense of the word. It’s more along the lines of a byte array for most programming languages. However, the IronPython array really doesn’t fit that description either. Rather, you tell IronPython what kind of sequence you want to store and then provide the storage values. Table 4-1 provides a list of the array element types.

IronPython Array Data Types

As you can see from the table, an array in IronPython doesn’t include any concept of a string. Strings in IronPython are considered a kind of sequence. To create an array of strings in IronPython, you’d need to create an array of character arrays. In short, the concept of an array is somewhat primitive in IronPython.

Arrays also differ from other array-like structures in IronPython in that you must provide a type as part of the array definition. Consequently, if you create a character array, you can’t suddenly decide to use it to store integer values. You must also import the array module to use IronPython arrays, because arrays aren’t part of the initial interpreter configuration. Listing 4-3 shows an example of an array in use.

Listin g 4-3: Working with an actual array in IronPython

[code]import array
# define a local version of array.
array = array.array
# Create a character array and assign it some values.
MyArray = array(‘c’, ‘Hello World’)
# Display each list element.
for TheCharacter in MyArray:
print TheCharacter
# Pause after the debug session.
raw_input(‘Press any key to continue…’)[/code]

The example begins by importing the array module and then assigning the array.array method to a local variable.

At this point, the code creates a character array. Now it may appear that this array contains a string, but what it really contains are individual characters. When you output the array, you see the individual characters, as shown in Figure 4-3. Consequently, most developers of other languages are going to view the IronPython array as a sort of byte array.

Arrays handle sequences of values.

After reading this section, you might find yourself a bit confused, especially if you’ve worked with Visual Basic.NET or C# in the past where arrays really are arrays. What you’ll want most often is a list in IronPython and you’ll likely use a tuple in some situations as well. When you think array for IronPython, think about these two kinds of objects. On the other hand, when you really do need to work with individual bits of data, then think about the IronPython array because it does work with those individual sequences.