Figure 5.7: Slicing doesn't modify lists.

list[i:j] is a slice of the original list from index i (inclusive) up to, but not including, index j (exclusive).4

The first index can be omitted if we want to slice from the beginning of the list, and the last index can be omitted if we want to slice to the end:

Download lists/celegans2.cmd

>>> celegans_markers = ['Emb', 'Him', 'Unc', 'Lon', 'Dpy', 'Sma']

To create a copy of the entire list, we just omit both indices so that the "slice" runs from the start of the list to its end:

Download lists/celegans3.cmd

>>> celegans_markers = ['Emb', 'Him', 'Unc', 'Lon', 'Dpy', 'Sma'] >>> celegans_copy = celegans_markers[:] >>> celegans_markers[5] = 'Lvl' >>> celegans_markers

['Emb', 'Him', 'Unc', 'Lon', 'Dpy', 'Lvl'] >>> celegans_copy

4. Python uses this convention to be consistent with the rule that the legal indices for a list go from 0 up to one less than the list's length.

Figure 5.8: Aliasing lists

5.6 Aliasing

An alias is an alternative name for something. In Python, two variables are said to be aliases when they refer to the same value. For example, the following code creates two variables, both of which refer to a single list (see Figure 5.8). When we modify the list using one of the variables, references through the other variable show the change as well:

Download lists/celegans4.cmd

>>> ce1egans_markers = ['Emb', 'Him', 'Unc', 'Lon', 'Dpy', 'Sma'] >>> ce1egans_copy = ce1egans_markers >>> ce1egans_markers[5] = 'Lvl' >>> celegans_markers

['Emb', 'Him', 'Unc', 'Lon', 'Dpy', 'Lvl'] >>> celegans_copy

Aliasing is one of the reasons why the notion of mutability is important. For example, if x and y refer to the same list, then any changes you make to the list through x will be "seen" by y, and vice versa. This can lead to all sorts of hard-to-find errors in which a list's value changes as if by magic, even though your program doesn't appear to assign anything to it. This can't happen with immutable values like strings. since a string can't be changed after it has been created, it's safe to have aliases for it.

Aliasing in Function Calls

Aliasing occurs when we use list parameters as well, since parameters are variables.


Appends value v to list L

Inserts value v at index i in list L, shifting following items to make room

Removes the first occurrence of value v from list L Reverses the order of the values in list L Sorts the values in list L in ascending order (for strings, alphabetical order)

Removes and returns the last element of L (which must be nonempty)

Figure 5.9: List methods

Here is a simple function that takes a list, sorts it, and then reverses it:

Download lists/alias_parameters.cmd

>>> celegans_markers = ['Emb', 'Him', 'Unc', 'Lon', 'Dpy' , 'Lvl'] >>> sort_and_reverse(celegans_markers) ['Unc', 'Lvl', 'Lon', 'Him', 'Emb', 'Dpy'] >>> celegans_markers

This function modifies list L, and since L is an alias of celegans_markers, that list is modified as well.

Was this article helpful?

0 0

Post a comment