Книга: Fedora™ Unleashed, 2008 edition
Lists
Lists
Python's built-in list data type is a sequence, like strings. However, Python's lists 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
['python', 'perl', 'php', 'python', 'perl', 'php']
>>> mylist[1]
'perl'
>>> mylist[1] = "c++"
>>> mylist[1]
'c++'
>>> mylist[1:3] ['c++', 'php']
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 a collection of characters that, after they are 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:
>>> list1 = [1, 2, 3]
>>> list2 = [4, list1, 6]
>>> list1
[1, 2, 3]
>>> list2
[4, [1, 2, 3], 6]
Here you can see a nested list. list2
contains 4, and then list1
, and then 6. When you print the value of list2, you can see it also contains list1. Now, proceeding on from that:
>>> list1[1] = "Flake"
>>> list2
[4, [1, 'Flake', 3], 6]
Line one sets the second element in list1
(remember, sequences are zero-based!) to be Flake
rather than 2; then the contents of list2
are printed. As you can see, when list1
changed, list2
was updated also. The reason for this is that list2
stores a reference to list1
as opposed to a copy of list1
; they share the same value.
You can show that this works both ways by indexing twice into list2
, like this:
>>> list2[1][1] = "Caramello"
>>> list1
[1, 'Caramello', 3]
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:
>>> mystring = "hello"
>>> list3 = [1, mystring, 3]
>>> list3
[1, 'hello', 3]
>>> mystring = "world"
>>> list3
[1, 'hello', 3]
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 you saw earlier. This slices from the first element to the last, inclusive, essentially copying it without refer ences. Here is how that looks:
>>> list4 = ["a", "b", "c"]
>>> list5 = list4[:]
>>> list4 = list4 + ["d"]
>>> list5
['a', 'b', 'c']
>>> list4
['a', 'b', 'c', 'd']
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 by which to split 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(" ")
['This', 'is', 'a', 'test', 'string']
Lists are used extensively in Python, although this is slowly changing as the language matures.
- Access Control Lists
- Mailing Lists
- Fedora Project Mailing Lists
- Red Hat Mailing Lists
- Linux Mailing Lists
- 6.3.3 Clearing Semaphore Task-Waiting Lists
- Playlists As Song Sources
- 13. Beyond Lists: Other Uses for Cons Cells
- Newsgroups and Mail Lists
- 11.5.2 More Lisp Basics: Lists
- Functional Programming and Lists
- Lookup Tables: Alists and Plists