In the previous examples, I’ve used lists quite a bit. You’ve seen how useful they are, but this section deals with what makes them different from tuples and strings: lists are mutable—that is, you can change their contents—and they have many useful specialized methods.
The list Function
Because strings can’t be modified in the same way as lists, sometimes 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.
1. It’s actually a type, not a function, but the difference isn’t important right
now.
Tip To convert a list of characters such as the preceding code back to a string,
you would use the following
expression:
''.join(somelist)
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 Chapter 1. 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]
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]. (Now why
don’t you try a slice assignment with a step size different from 1? Perhaps even a negative one?)
on our next topic we will been discussing about "LIST METHOD"...
HAPPY CODING SEASON
Tuesday, November 21, 2017
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment