Lists

Python's built-in list data type is a sequence, like strings. However, they are mutable, which means they can be changed. Lists are like arrays in that they hold a selection of elements in a given order. You can cycle through them, index into them, and slice them:

>>> mylist = ["python", "perl", "php"] >>> mylist

['python', 'perl', 'php'] >>> mylist + ["java"] ['python', 'perl', 'php', 'java'] >>> mylist * 2

The brackets notation is important: You cannot use parentheses, (( and )) or braces ({ and }) for lists. Using + for lists is different from using + for numbers. Python detects you are working with a list and appends one list to another. This is known as operator overloading, and it is one of the reasons Python is so flexible.

Lists can be nested, which means you can put a list inside a list. However, this is where mutability starts to matter, and so this might sound complicated! If you recall, the definition of an immutable string sequence is that it is a collection of characters that, once set, cannot be changed without creating a new string. Lists are mutable as opposed to immutable, which means you can change your list without creating a new list.

This becomes important because Python, by default, copies only a reference to a variable rather than the full variable. For example:

Here we have a nested list. list2 contains 4, then l isti, then 6. When you print the value of list2, you can see it also contains listi. Now, proceeding on from that:

>>> list1[1] = "Flake" >>> list2

In line one, we set the second element in list1 (remember, sequences are zero based!) to be Flake rather than 2; then we print the contents of list2. As you can see, when list1 changed, list2 was updated, too. The reason for this is that list2 stores a reference to list1 as opposed to a copy of list1; they share the same value.

We can show that this works both ways by indexing twice into list2, like this:

>>> list2[1][1] = "Caramello" >>> list1

The first line says, "Get the second element in list2 (list1) and the second element of that list, and set it to be 'Caramello'." Then list1's value is printed, and you can see it has changed. This is the essence of mutability: We are changing our list without creating a new list. On the other hand, editing a string creates a new string, leaving the old one unaltered. For example:

Of course, this raises the question of how you copy without references when references are the default. The answer, for lists, is that you use the [:] slice, which we looked at earlier. This slices from the first element to the last, inclusive, essentially copying it without references. Here is how that looks:

>>> list4 = ["a", "b", "c"]

Lists have their own collections of built-in methods, such as sort(), append(), and pop(). The latter two add and remove single elements from the end of the list, with pop() also returning the removed element. For example:

>>> list5 = ["nick", "paul", "julian", "graham"] >>> list5.sort() >>> list5

['graham', 'julian', 'nick', 'paul'] >>> list5.pop() 'paul' >>> list5

['graham', 'julian', 'nick'] >>> list5.append("Rebecca")

In addition, one interesting method of strings returns a list: split(). This takes a character to split by and then gives you a list in which each element is a chunk from the string. For example:

>>> string = "This is a test string"; >>> string.split(" ")

Lists are used extensively in Python, although this is slowly changing as the language matures.

Was this article helpful?

0 0

Post a comment