Thursday, December 7, 2017

list methods in python part2 at codelent



remove
The remove method is used to remove the first occurrence of a value:
>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x.remove('bee')
Traceback (innermost last):
File "<pyshell#3>", line 1, in ?
x.remove('bee')
ValueError: list.remove(x): x not in list
As you can see, only the first occurrence is removed, and you cannot remove something
(in this case, the string 'bee') if it isn’t in the list to begin with.
It’s important to note that this is one of the “nonreturning in-place changing” methods. It
modifies the list, but returns nothing (as opposed to pop).
reverse
The reverse method reverses the elements in the list. (Not very surprising, I guess.)
>>> x = [1, 2, 3]
>>> x.reverse()
>>> x
[3, 2, 1]
Note that reverse changes the list and does not return anything (just like remove and sort,
for example).

¦Tip If you want to iterate over a sequence in reverse, you can use the reversed function. This function
doesn’t return a list, though; it returns an iterator. (You learn more about iterators in Chapter 9.) You can convert
the returned object with list:
>>> x = [1, 2, 3]
>>> list(reversed(x))
[3, 2, 1]
sort
The sort method is used to sort lists in place.3 Sorting “in place” means changing the original
list so its elements are in sorted order, rather than simply returning a sorted copy of the list:
>>> x = [4, 6, 2, 1, 7, 9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]
You’ve encountered several methods already that modify the list without returning anything,
and in most cases that behavior is quite natural (as with append, for example). But I want
to emphasize this behavior in the case of sort because so many people seem to be confused by
it. The confusion usually occurs when users want a sorted copy of a list while leaving the original
alone. An intuitive (but wrong) way of doing this is as follows:
>>> x = [4, 6, 2, 1, 7, 9]
>>> y = x.sort() # Don't do this!
>>> print y
None
Because sort modifies x but returns nothing, you end up with a sorted x and a y containing
None. One correct way of doing this would be to first bind y to a copy of x, and then sort y, as follows:
>>> x = [4, 6, 2, 1, 7, 9]
>>> y = x[:]
>>> y.sort()
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
Recall that x[:] is a slice containing all the elements of x, effectively a copy of the entire
list. Simply assigning x to y wouldn’t work because both x and y would refer to the same list:
>>> y = x
>>> y.sort()
3. In case you’re interested: from Python 2.3 on, the sort method uses a stable sorting algorithm.

>>> x
[1, 2, 4, 6, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
Another way of getting a sorted copy of a list is using the sorted function:
>>> x = [4, 6, 2, 1, 7, 9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
This function can actually be used on any sequence, but will always return a list:4
>>> sorted('Python')
['P', 'h', 'n', 'o', 't', 'y']
If you want to sort the elements in reverse order, you can use sort (or sorted), followed by
a call to the reverse method, or you could use the reverse argument, described in the following
section.
Advanced Sorting
If you want to have your elements sorted in a specific manner (other than sort’s default behavior,
which is to sort elements in ascending order, according to Python’s default comparison
rules, as explained in Chapter 5), you can define your own comparison function, of the form
compare(x,y), which returns a negative number when x < y, a positive number when x > y,
and zero when x == y (according to your definition). You can then supply this as a parameter
to sort. The built-in function cmp provides the default behavior:
>>> cmp(42, 32)
1
>>> cmp(99, 100)
-1
>>> cmp(10, 10)
0
>>> numbers = [5, 2, 9, 7]
>>> numbers.sort(cmp)
>>> numbers
[2, 5, 7, 9]
The sort method has two other optional arguments: key and reverse. If you want to use
them, you normally specify them by name (so-called keyword arguments; you learn more about
those in Chapter 6). The key argument is similar to the cmp argument: you supply a function and
it’s used in the sorting process. However, instead of being used directly for determining whether

one element is smaller than another, the function is used to create a key for each element, and the
elements are sorted according to these keys. So, for example, if you want to sort the elements
according to their lengths, you use len as the key function:
>>> x = ['aardvark', 'abalone', 'acme', 'add', 'aerate']
>>> x.sort(key=len)
>>> x
['add', 'acme', 'aerate', 'abalone', 'aardvark']
The other keyword argument, reverse, is simply a truth value (True or False; you learn
more about these in Chapter 5) indicating whether the list should be sorted in reverse:
>>> x = [4, 6, 2, 1, 7, 9]
>>> x.sort(reverse=True)
>>> x
[9, 7, 6, 4, 2, 1]
The cmp, key, and reverse arguments are available in the sorted function as well. In many
cases, using custom functions for cmp or key will be useful

0 comments:

Post a Comment