-->

The List Data Type [Python]

One more topic you’ll need to understand before you can begin writing programs in earnest is the list data type and its cousin, the tuple. Lists and tuples can contain multiple values, which makes it easier to write programs that handle large amounts of data. And since lists themselves can contain other lists, you can use them to arrange data into hierarchical structures.



In this chapter, I’ll discuss the basics of lists. I’ll also teach you about methods, which are functions that are tied to values of a certain data type. Then I’ll briefly cover the list-like tuple and string data types and how they compare to list values. In the next chapter, I’ll introduce you to the dictionary data type.

The List Data Type

A list is a value that contains multiple values in an ordered sequence. The term list value refers to the list itself (which is a value that can be stored in a variable or passed to a function like any other value), not the values inside the list value. A list value looks like this: ['cat', 'bat', 'rat', 'elephant']. Just as string values are typed with quote characters to mark where the string begins and ends, a list begins with an opening square bracket and ends with a closing square bracket, []. Values inside the list are also called items. Items are separated with commas (that is, they are comma-delimited). For example, enter the following into the interactive shell:

  1. >>> [1, 2, 3]
  2. [1, 2, 3]
  3. >>> ['cat', 'bat', 'rat', 'elephant']
  4. ['cat', 'bat', 'rat', 'elephant']
  5. >>> ['hello', 3.1415, True, None, 42]
  6. ['hello', 3.1415, True, None, 42]
  7. >>> spam = ['cat', 'bat', 'rat', 'elephant']
  8. >>> spam
  9. ['cat', 'bat', 'rat', 'elephant']

The spam variable u is still assigned only one value: the list value. But the list value itself contains other values. The value [] is an empty list that contains no values, similar to '', the empty string.

Getting Individual Values in a List with Indexes

Say you have the list ['cat', 'bat', 'rat', 'elephant'] stored in a variable named spam. The Python code spam[0] would evaluate to 'cat', and spam[1] would evaluate to 'bat', and so on. The integer inside the square brackets that follows the list is called an index. The first value in the list is at index 0, the second value is at index

A list value stored in the variable spam, showing which value each index refers to


1, the third value is at index 2, and so on. Figure 4-1 shows a list value assigned to spam, along with what the index expressions would evaluate to. For example, type the following expressions into the interactive shell. Start by assigning a list to the variable spam.

  1. >>> spam = ['cat', 'bat', 'rat', 'elephant']
  2. >>> spam[0]
  3. 'cat'
  4. >>> spam[1]
  5. 'bat'
  6. >>> spam[2]
  7. 'rat'
  8. >>> spam[3]
  9. 'elephant'
  10. >>> ['cat', 'bat', 'rat', 'elephant'][3]
  11. 'elephant'
  12. >>> 'Hello ' + spam[0]
  13. 'Hello cat'
  14. >>> 'The ' + spam[1] + ' ate the ' + spam[0] + '.'
  15. 'The bat ate the cat.'

Notice that the expression 'Hello ' + spam[0] u evaluates to 'Hello ' + 'cat' because spam[0] evaluates to the string 'cat'. This expression in turn evaluates to the string value 'Hello cat' v. Python will give you an IndexError error message if you use an index that exceeds the number of values in your list value.

  1. >>> spam = ['cat', 'bat', 'rat', 'elephant']
  2. >>> spam[10000]
  3. Traceback (most recent call last):
  4. File "<pyshell#9>", line 1, in <module>
  5. spam[10000]
  6. IndexError: list index out of range

Indexes can be only integer values, not floats. The following example will cause a TypeError error:

  1. >>> spam = ['cat', 'bat', 'rat', 'elephant']
  2. >>> spam[1]
  3. 'bat'
  4. >>> spam[1.0]
  5. Traceback (most recent call last):
  6. File "<pyshell#13>", line 1, in <module>
  7. spam[1.0]
  8. TypeError: list indices must be integers, not float
  9. >>> spam[int(1.0)]
  10. 'bat'

Lists can also contain other list values. The values in these lists of lists can be accessed using multiple indexes, like so:

  1. >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
  2. >>> spam[0]
  3. ['cat', 'bat']
  4. >>> spam[0][1]
  5. 'bat'
  6. >>> spam[1][4]
  7. 50

The first index dictates which list value to use, and the second indicates the value within the list value. For example, spam[0][1] prints 'bat', the second value in the first list. If you only use one index, the program will print the full list value at that index.



0 Response to "The List Data Type [Python]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel