Partially unpacking a sequence

tkpmep at hotmail.com tkpmep at hotmail.com
Thu Apr 6 16:58:39 EDT 2006


Thank you, everyone, for resolving my question. At one point, while
trying to solve the problem, I typed

>>> y[1,3]
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: list indices must be integers

The error message gave me no clue as to what I was doing wrong (in my
mind, I was just writing out the elements of a range), and I thought
perhaps that my inclusion of a comma was the problem. Perhaps a more
explicit error message would have helped.

Another solution, I suppose, is to use a list comprehension. Store the
indices in a list x. For example, let x = [1,5,4]. Then

a,b,c = [y[i] for i in x]

Thanks

Thomas Philips




More information about the Python-list mailing list