Think Python for CS114

Chapter 20 List operations

The + operator concatenates lists:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> c
[1, 2, 3, 4, 5, 6]

The * operator repeats a list a given number of times:

>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.

20.1 List slices

The slice operator also works on lists:

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3]
['b', 'c']
>>> t[:4]
['a', 'b', 'c', 'd']
>>> t[3:]
['d', 'e', 'f']

If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end. So if you omit both, the slice contains all the items from the list. (But it’s a new list!)

>>> t[:]
['a', 'b', 'c', 'd', 'e', 'f']
>>> t == t[:]
True
>>> t is t[:]
False

A slice operator on the left side of an assignment can update multiple elements:

>>> t = ['a', 'b', 'c', 'd', 'e', 'f']
>>> t[1:3] = ['x', 'y']
>>> t
['a', 'x', 'y', 'd', 'e', 'f']

We can use slicing to create a new list containing all the same items that are in the old list:

>>> t1 = ['a', 'b', 'c']
>>> t2 = t1[:]  # Slice the whole list: from the first, to the last.
>>> t1[0] = 'q'
>>> t1
['q', 'b', 'c']
>>> t2
['a', 'b', 'c']

Since lists are mutable, it is often useful to make a copy before performing operations that modify lists.

20.2 List methods

Python provides methods that operate on lists.

append is called on a list. It takes a value and mutates the list, adding the new element to the end:

>>> t = ['a', 'b', 'c']
>>> t.append('d')
>>> t
['a', 'b', 'c', 'd']

extend is called on a list. It takes a list and mutates the first list, appending all the values in the second list to it:

>>> t1 = ['a', 'b', 'c']
>>> t2 = ['d', 'e']
>>> t1.extend(t2)
>>> t1
['a', 'b', 'c', 'd', 'e']

This example leaves t2 unmodified.

Most list methods simply modify the list and return None. If you accidentally write t = t.append(42), t will end up containing None.

20.3 Deleting elements

There are several ways to delete elements from a list, but we need only one: the pop method.

>>> t = ['a', 'b', 'c']
>>> x = t.pop(1)
>>> t
['a', 'c']
>>> x
'b'

pop modifies the list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element.

>>> t = ['a', 'b', 'c']
>>> x = t.pop()
>>> t
['a', 'b']
>>> x
'c'

20.4 Lists and strings

A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a string to a list of characters, you can use list:

>>> s = 'spam'
>>> t = list(s)
>>> t
['s', 'p', 'a', 'm']

Because list is the name of a built-in function, you should avoid using it as a variable name. I also avoid l because it looks too much like 1. So that’s why I use t.

The list function breaks a string into individual letters. If you want to break a string into words, you can use the split method:

>>> s = 'pining for the fjords'
>>> t = s.split()
>>> t
['pining', 'for', 'the', 'fjords']

An optional argument called a delimiter specifies which characters to use as word boundaries. The following example uses a hyphen as a delimiter:

>>> s = 'spam-spam-spam'
>>> delimiter = '-'
>>> t = s.split(delimiter)
>>> t
['spam', 'spam', 'spam']

join is the inverse of split. It takes a list of strings and concatenates the elements. join is a string method, so you have to invoke it on the delimiter and pass the list as a parameter:

>>> t = ['pining', 'for', 'the', 'fjords']
>>> delimiter = ' '
>>> s = delimiter.join(t)
>>> s
'pining for the fjords'

In this case the delimiter is a space character, so join puts a space between words. To concatenate strings without spaces, you can use the empty string, ’’, as a delimiter.