Online Book Reader

Home Category

Learning Python - Mark Lutz [169]

By Root 1791 0
sense of this, you need to know that the range built-in function generates a list of successive integers:

>>> range(3) # Use list(range(3)) in Python 3.0

[0, 1, 2]

Because range is commonly used in for loops, we’ll say more about it in Chapter 13.

Another place you may see a tuple assignment at work is for splitting a sequence into its front and the rest in loops like this:

>>> L = [1, 2, 3, 4]

>>> while L:

... front, L = L[0], L[1:] # See next section for 3.0 alternative

... print(front, L)

...

1 [2, 3, 4]

2 [3, 4]

3 [4]

4 []

The tuple assignment in the loop here could be coded as the following two lines instead, but it’s often more convenient to string them together:

... front = L[0]

... L = L[1:]

Notice that this code is using the list as a sort of stack data structure, which can often also be achieved with the append and pop methods of list objects; here, front = L.pop(0) would have much the same effect as the tuple assignment statement, but it would be an in-place change. We’ll learn more about while loops, and other (often better) ways to step through a sequence with for loops, in Chapter 13.

Extended Sequence Unpacking in Python 3.0

The prior section demonstrated how to use manual slicing to make sequence assignments more general. In Python 3.0 (but not 2.6), sequence assignment has been generalized to make this easier. In short, a single starred name, *X, can be used in the assignment target in order to specify a more general matching against the sequence—the starred name is assigned a list, which collects all items in the sequence not assigned to other names. This is especially handy for common coding patterns such as splitting a sequence into its “front” and “rest”, as in the preceding section’s last example.

Extended unpacking in action

Let’s look at an example. As we’ve seen, sequence assignments normally require exactly as many names in the target on the left as there are items in the subject on the right. We get an error if the lengths disagree (unless we manually sliced on the right, as shown in the prior section):

C:\misc> c:\python30\python

>>> seq = [1, 2, 3, 4]

>>> a, b, c, d = seq

>>> print(a, b, c, d)

1 2 3 4

>>> a, b = seq

ValueError: too many values to unpack

In Python 3.0, though, we can use a single starred name in the target to match more generally. In the following continuation of our interactive session, a matches the first item in the sequence, and b matches the rest:

>>> a, *b = seq

>>> a

1

>>> b

[2, 3, 4]

When a starred name is used, the number of items in the target on the left need not match the length of the subject sequence. In fact, the starred name can appear anywhere in the target. For instance, in the next interaction b matches the last item in the sequence, and a matches everything before the last:

>>> *a, b = seq

>>> a

[1, 2, 3]

>>> b

4

When the starred name appears in the middle, it collects everything between the other names listed. Thus, in the following interaction a and c are assigned the first and last items, and b gets everything in between them:

>>> a, *b, c = seq

>>> a

1

>>> b

[2, 3]

>>> c

4

More generally, wherever the starred name shows up, it will be assigned a list that collects every unassigned name at that position:

>>> a, b, *c = seq

>>> a

1

>>> b

2

>>> c

[3, 4]

Naturally, like normal sequence assignment, extended sequence unpacking syntax works for any sequence types, not just lists. Here it is unpacking characters in a string:

>>> a, *b = 'spam'

>>> a, b

('s', ['p', 'a', 'm'])

>>> a, *b, c = 'spam'

>>> a, b, c

('s', ['p', 'a'], 'm')

This is similar in spirit to slicing, but not exactly the same—a sequence unpacking assignment always returns a list for multiple matched items, whereas slicing returns a sequence of the same type as the object sliced:

>>> S = 'spam'

>>> S[0], S[1:] # Slices are type-specific, * assignment always returns a list

('s', 'pam')

>>> S[0], S[1:3], S[3]

('s', 'pa', 'm')

Given this extension in 3.0, as long as we’re processing a list

Return Main Page Previous Page Next Page

®Online Book Reader