Partially unpacking a sequence

T

tkpmep

I have a list y['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']

from which I want to extract only the 2nd and 4th item by partially
unpacking the list. So I tried
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers

Out of curiosity, I tried
'18.500'

Why does this work (to a point - it gives me items 2 and 3, not 2 and
4 as I require) and not my first attempt? What is the right syntax to
use when partially upacking a sequence?

Thanks in advance

Thomas Philips
 
P

Paul McGuire

I have a list y['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']

from which I want to extract only the 2nd and 4th item by partially
unpacking the list. So I tried
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers

Out of curiosity, I tried
a,b = y[2:4]
a '04276410'
b
'18.500'

Why does this work (to a point - it gives me items 2 and 3, not 2 and
4 as I require) and not my first attempt? What is the right syntax to
use when partially upacking a sequence?

Thanks in advance

Thomas Philips

a,b = y[2],y[4]

or

a,b = y[2:5:2]

or

a,b = ( y for i in (2,4) )


-- Paul
 
F

Fredrik Lundh

I have a list y['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']

from which I want to extract only the 2nd and 4th item by partially
unpacking the list. So I tried
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers

Out of curiosity, I tried
a,b = y[2:4]
a '04276410'
b
'18.500'

Why does this work (to a point - it gives me items 2 and 3, not 2 and
4 as I require) and not my first attempt? What is the right syntax to
use when partially upacking a sequence?

if you want two items, fetch two items:

a = y[2]
b = y[4]

y[2:4] is a 2-item slice starting at index 2 and ending *before* index 4.
see the documentation for more on slicing.

</F>
 
P

Paul McGuire

Paul McGuire said:
I have a list y
['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']

from which I want to extract only the 2nd and 4th item by partially
unpacking the list. So I tried
a,b = y[2,4]
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: list indices must be integers

Out of curiosity, I tried
a,b = y[2:4]
a '04276410'
b
'18.500'

Why does this work (to a point - it gives me items 2 and 3, not 2 and
4 as I require) and not my first attempt? What is the right syntax to
use when partially upacking a sequence?

Thanks in advance

Thomas Philips

a,b = y[2],y[4]

or

a,b = y[2:5:2]

or

a,b = ( y for i in (2,4) )


-- Paul


Forgot one:

_,_,a,_,b,_,_,_ = y

There actually is some merit to this form. If the structure of y changes
sometime in the future (specifically if a field is added or removed), this
statement will fail noisily, calling your attention to this change. But if
a new field is added, say at the front of the list, the previous methods
will all silently succeed, but now giving you the values formerly known as
y[1] and y[3].

-- Paul
 
T

tkpmep

Thank you, everyone, for resolving my question. At one point, while
trying to solve the problem, I typed
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 for i in x]

Thanks

Thomas Philips
 
F

Fredrik Lundh

Paul said:
Forgot one:

_,_,a,_,b,_,_,_ = y

There actually is some merit to this form. If the structure of y changes
sometime in the future (specifically if a field is added or removed), this
statement will fail noisily, calling your attention to this change. But if
a new field is added, say at the front of the list, the previous methods
will all silently succeed, but now giving you the values formerly known as
y[1] and y[3].

if this is likely to happen, an straightforward assert is a lot easier to parse than
a one-two-a-three-oops-one-two-a-four-five-etc number of underscores:

assert len(y) == 8
a = y[2]
b = y[4]

</F>
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
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.

You thought correct. List indices must be integers, not tuples.
Perhaps a more explicit error message would have helped.

Why? You thought the right thing.

Sybren
 
J

jUrner

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

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.


The error message is correct because in y[1, 3] "1, 3" is recognized by
the interpreter
as tuple. Python goodie or snakebite that is...
 
F

Fredrik Lundh

Thank you, everyone, for resolving my question. At one point, while
trying to solve the problem, I typed
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.

the problem is that the *compiler* doesn't know what "y" is, and y[1,3]
is a perfectly valid way to access e.g. a dictionary or a multidimensional
array. so it's the list implementation that has to do the complaining, and
all it knows is that it wants an integer index, and got something else.

maybe something like

TypeError: list indices must be integers (got tuple)

would have been less confusing; feel free to add a suggestion to the bug
tracker:

http://sourceforge.net/tracker/?group_id=5470&atid=105470

</F>
 
B

bruno at modulix

I have a list y

['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500',
'19.500', '224']

from which I want to extract only the 2nd and 4th item

by partially
unpacking the list. So I tried

Mmm, so lovely and meaningful names !-)

FWIW, and since nobody seemed to mention it, list indexes are
zero-based, so the second element of a list is at index 1 and the fourth
at index 3.

Also, a GoodPractice(tm) is to use named constants instead of magic
numbers. Here we don't have a clue about why these 2 elements are so
specials. Looking at the example list (which - semantically - should be
a tuple, not a list) I could wild-guess that the 2nd item is a reference
and the fourth a price, so:

REF_INDEX = 1 # lists are zero-based
PRICE_INDEX = 3

ref, price = y[REF_INDEX], y[PRICE_INDEX]

And finally, since this list is clearly structured data, wrapping it
into an object to hide away implementation details *may* help -
depending on the context, of course !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,171
Latest member
EllaHolmwo

Latest Threads

Top