Thursday, December 7, 2017

List Methods in python part1 at codelent

You’ve encountered functions already, but now it’s time to meet a close relative: methods.
A method is a function that is tightly coupled to some object, be it a list, a number, a string,
or whatever. In general, a method is called like this:
object.method(arguments)
As you can see, a method call looks just like a function call, except that the object is put
before the method name, with a dot separating them. (You get a much more detailed explanation
of what methods really are in Chapter 7.)
Lists have several methods that allow you to examine or modify their contents.
append
The append method is used to append an object to the end of a list:
>>> lst = [1, 2, 3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
You might wonder why I have chosen such an ugly name as lst for my list. Why not call it list?
I could do that, but as you might remember, list is a built-in function.2 If I use the name for a list
instead, I won’t be able to call the function anymore. You can generally find better names for a
given application. A name such as lst really doesn’t tell you anything. So if your list is a list of prices,
for instance, you probably ought to call it something like prices, prices_of_eggs, or pricesOfEggs.
It’s also important to note that append, like several similar methods, changes the list in
place. This means that it does not simply return a new, modified list; instead, it modifies the old
one directly. This is usually what you want, but it may sometimes cause trouble. I’ll return to
this discussion when I describe sort later in the chapter.
count
The count method counts the occurrences of an element in a list:
>>> ['to', 'be', 'or', 'not', 'to', 'be'].count('to')
2
>>> x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
>>> x.count(1)
2
>>> x.count([1, 2])
1

extend
The extend method allows you to append several values at once by supplying a sequence of the
values you want to append. In other words, your original list has been extended by the other one:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
This may seem similar to concatenation, but the important difference is that the extended
sequence (in this case, a) is modified. This is not the case in ordinary concatenation, in which
a completely new sequence is returned:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]
As you can see, the concatenated list looks exactly the same as the extended one in the previous
example, yet a hasn’t changed this time. Because ordinary concatenation must make a
new list that contains copies of a and b, it isn’t quite as efficient as using extend if what you want
is something like this:
>>> a = a + b
Also, this isn’t an in-place operation—it won’t modify the original.
The effect of extend can be achieved by assigning to slices, as follows:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a[len(a):] = b
>>> a
[1, 2, 3, 4, 5, 6]
While this works, it isn’t quite as readable.
index
The index method is used for searching lists to find the index of the first occurrence of a value:
>>> knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni']
>>> knights.index('who')
4
>>> knights.index('herring')
Traceback (innermost last):
File "<pyshell#76>", line 1, in ?
knights.index('herring')
ValueError: list.index(x): x not in list

When you search for the word 'who', you find that it’s located at index 4:
>>> knights[4]
'who'
However, when you search for 'herring', you get an exception because the word is not
found at all.
insert
The insert method is used to insert an object into a list:
>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers.insert(3, 'four')
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]
As with extend, you can implement insert with slice assignments:
>>> numbers = [1, 2, 3, 5, 6, 7]
>>> numbers[3:3] = ['four']
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]
This may be fancy, but it is hardly as readable as using insert.
pop
The pop method removes an element (by default, the last one) from the list and returns it:
>>> x = [1, 2, 3]
>>> x.pop()
3
>>> x
[1, 2]
>>> x.pop(0)
1
>>> x
[2]
¦Note The pop method is the only list method that both modifies the list and returns a value
(other than None).
Using pop, you can implement a common data structure called a stack. A stack like this
works just like a stack of plates. You can put plates on top, and you can remove plates from the
top. The last one you put into the stack is the first one to be removed. (This principle is called
last-in, first-out, or LIFO.)
The generally accepted names for the two stack operations (putting things in and taking
them out) are push and pop. Python doesn’t have push, but you can use append instead. The pop
and append methods reverse each other’s results, so if you push (or append) the value you just
popped, you end up with the same stack:
>>> x = [1, 2, 3]
>>> x.append(x.pop())
>>> x
[1, 2, 3]

note: If you want a first-in, first-out (FIFO) queue, you can use insert(0, ...) instead of append. Alternatively,
you could keep using append but substitute pop(0) for pop(). An even better solution would be to
use a deque from the collections module. See Chapter 10 for more information.

0 comments:

Post a Comment