Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 2 ■ LISTS AND TUPLES 41 The list Function Because strings can’t be modified in the same way as lists, often it can be useful to create a list from a string. You can do this with the list function: 1 >>> list('Hello') ['H', 'e', 'l', 'l', 'o'] Note that list works with all kinds of sequences, not just strings. ■Tip To convert a list of characters such as the preceding code back to a string, you would use the following expression: ''.join(somelist) where somelist is your list. For an explanation of what this really means, see the section about join in Chapter 3. Basic List Operations You can perform all the standard sequence operations on lists, such as indexing, slicing, concatenating, and multiplying; but the interesting thing about lists is that they can be modified. In this section, you see some of the ways you can change a list: item assignments, item deletion, slice assignments, and list methods. (Note that not all list methods actually change their list.) Changing Lists: Item Assignments Changing a list is easy. You just use ordinary assignment as explained in the first chapter. However, instead of writing something like x = 2, you use the indexing notation to assign to a specific, existing position, such as x[1] = 2. >>> x = [1, 1, 1] >>> x[1] = 2 >>> x [1, 2, 1] ■Note You cannot assign to a position that doesn’t exist; if your list is of length 2, you cannot assign a value to index 100. To do that, you would have to make a list of length 101 (or more). See the section “None, Empty Lists, and Initialization,” earlier in this chapter. 1. It’s actually a type, not a function, but the difference isn’t important right now.
42 CHAPTER 2 ■ LISTS AND TUPLES Deleting Elements Deleting elements from a list is easy too; you can simply use the del statement: >>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl'] >>> del names[2] >>> names ['Alice', 'Beth', 'Dee-Dee', 'Earl'] Notice how Cecil is completely gone, and the length of the list has shrunk from five to four. The del statement may be used to delete things other than list elements. It can be used with dictionaries (see Chapter 4) or even variables. For more information, see Chapter 5. Assigning to Slices Slicing is a very powerful feature, and it is made even more powerful by the fact that you can assign to slices: >>> name = list('Perl') >>> name ['P', 'e', 'r', 'l'] >>> name[2:] = list('ar') >>> name ['P', 'e', 'a', 'r'] So you can assign to several positions at once. You may wonder what the big deal is. Couldn’t you just have assigned to them one at a time? Sure, but when you use slice assignments, you may also replace the slice with a sequence whose length is different from that of the original: >>> name = list('Perl') >>> name[1:] = list('ython') >>> name ['P', 'y', 't', 'h', 'o', 'n'] Slice assignments can even be used to insert elements without replacing any of the original ones: >>> numbers = [1, 5] >>> numbers[1:1] = [2, 3, 4] >>> numbers [1, 2, 3, 4, 5] Here, I basically “replaced” an empty slice, thereby really inserting a sequence. You can do the reverse to delete a slice: >>> numbers [1, 2, 3, 4, 5] >>> numbers[1:4] = [] >>> numbers [1, 5] As you may have guessed, this last example is equivalent to del numbers[1:4].
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 36 and 37: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
42 CHAPTER 2 ■ LISTS AND TUPLES<br />
Deleting Elements<br />
Deleting elements from a list is easy <strong>to</strong>o; you can simply use the del statement:<br />
>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']<br />
>>> del names[2]<br />
>>> names<br />
['Alice', 'Beth', 'Dee-Dee', 'Earl']<br />
Notice how Cecil is completely gone, and the length of the list has shrunk from five <strong>to</strong> four.<br />
The del statement may be used <strong>to</strong> delete things other than list elements. It can be used<br />
with dictionaries (see Chapter 4) or even variables. For more information, see Chapter 5.<br />
Assigning <strong>to</strong> Slices<br />
Slicing is a very powerful feature, and it is made even more powerful by the fact that you can<br />
assign <strong>to</strong> slices:<br />
>>> name = list('Perl')<br />
>>> name<br />
['P', 'e', 'r', 'l']<br />
>>> name[2:] = list('ar')<br />
>>> name<br />
['P', 'e', 'a', 'r']<br />
So you can assign <strong>to</strong> several positions at once. You may wonder what the big deal is. Couldn’t<br />
you just have assigned <strong>to</strong> them one at a time? Sure, but when you use slice assignments, you<br />
may also replace the slice with a sequence whose length is different from that of the original:<br />
>>> name = list('Perl')<br />
>>> name[1:] = list('ython')<br />
>>> name<br />
['P', 'y', 't', 'h', 'o', 'n']<br />
Slice assignments can even be used <strong>to</strong> insert elements without replacing any of the<br />
original ones:<br />
>>> numbers = [1, 5]<br />
>>> numbers[1:1] = [2, 3, 4]<br />
>>> numbers<br />
[1, 2, 3, 4, 5]<br />
Here, I basically “replaced” an empty slice, thereby really inserting a sequence. You can do<br />
the reverse <strong>to</strong> delete a slice:<br />
>>> numbers<br />
[1, 2, 3, 4, 5]<br />
>>> numbers[1:4] = []<br />
>>> numbers<br />
[1, 5]<br />
As you may have guessed, this last example is equivalent <strong>to</strong> del numbers[1:4].