Modifying lists

You can use list index notation to modify a list as well as to extract an element from it. Put the index on the left side of the assignment operator:

>>> x = [1, 2, 3, 4] >>> x[1] = "two" >>> x

Slice notation can be used here too. Saying something like lista[index1: index2] = listb causes all elements of lista between index1 and index2 to be replaced with the elements in listb. listb can have more or fewer elements than are removed from lista, in which case the length of lista will be altered. You can use slice assignment to do a number of different things, as shown here:

>>> x = [1, 2, 3, 4] >>> x[len(x):] = [5, 6 >>> x

[1, 2, 3, 4, 5, 6, 7] >>> x[:0] = [-1, 0] >>> x

[-1, 0, 1, 2, 3, 4 >>> x[1:-1] = [] >>> x [-1, 7]

Appends list to end of list

Appends list to front of list

Removes elements from list

Appending a single element to a list is such a common operation that there's a special append method to do it:

>>> x = [1, 2, 3] >>> x.append("four") >>> x

One problem can occur if you try to append one list to another. The list gets appended as a single element of the main list:

>>> x = [1, 2, 3, 4] >>> y = [5, 6, 7] >>> x.append(y) >>> x

The extend method is like the append method, except that it allows you to add one list to another:

>>> x = [1, 2, 3, 4] >>> y = [5, 6, 7] >>> x.extend(y) >>> x

There is also a special insert method to insert new list elements between two existing elements or at the front of the list. insert is used as a method of lists and takes two additional arguments; the first is the index position in the list where the new element should be inserted, and the second is the new element itself:

insert understands list indices as discussed in the section on slice notation, but for most uses it's easiest to think of list, insert (n, elem) as meaning insert elem just before the nth element of list. insert is just a convenience method. Anything that can be done with insert can also be done using slice assignment; that is, list .insert(n, elem) is the same thing as list[n:n] = [elem] when n is nonnegative. Using insert makes for somewhat more readable code, and insert even handles negative indices:

The del statement is the preferred method of deleting list items or slices. It doesn't do anything that can't be done with slice assignment, but it's usually easier to remember and easier to read:

>>> x = ['a', 2, 'c', 7, 9, 11] >>> del x[1] >>> x

['a', 'c', 7, 9, 11] >>> del x[:2] >>> x [7, 9, 11]

In general, del list [n] does the same thing as list[n:n+1] = [], whereas del list[m:n] does the same thing as list[m:n] = [].

The remove method isn't the converse of insert. Whereas insert inserts an element at a specified location, remove looks for the first instance of a given value in a list and removes that value from the list:

>>> x = [1, 2, 3, 4, 3, 5] >>> x.remove(3) >>> x

[1, 2, 4, 3, 5] >>> x.remove(3) >>> x

Traceback (innermost last):

File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list

If remove can't find anything to remove, it raises an error. You can catch this error using the exception-handling abilities of Python, or you can avoid the problem by using in to check for the presence of something in a list before attempting to remove it.

The reverse method is a more specialized list modification method. It efficiently reverses a list in place:

Was this article helpful?

0 0

Post a comment