469,327 Members | 1,275 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Partially unpacking a sequence

I have a list y
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 trieda,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 trieda,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

Apr 6 '06 #1
9 1292
<tk****@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
I have a list y
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 trieda,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 trieda,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[i] for i in (2,4) )
-- Paul
Apr 6 '06 #2
tk****@hotmail.com wrote:
I have a list y
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 trieda,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 trieda,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>

Apr 6 '06 #3
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:Fk*****************@tornado.texas.rr.com...
<tk****@hotmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
I have a list y
>>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[i] 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
Apr 6 '06 #4
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

Apr 6 '06 #5
Paul McGuire wrote:
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>

Apr 6 '06 #6
tk****@hotmail.com enlightened us with:
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.


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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 6 '06 #7
>> 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.

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...

Apr 6 '06 #8
tk****@hotmail.com wrote:
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.


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/?grou...70&atid=105470

</F>

Apr 7 '06 #9
tk****@hotmail.com wrote:
I have a list y
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]


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 !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 7 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by Dave Benjamin | last post: by
2 posts views Thread by george young | last post: by
8 posts views Thread by Paul McGuire | last post: by
5 posts views Thread by Chris | last post: by
11 posts views Thread by harold | last post: by
16 posts views Thread by John Salerno | last post: by
4 posts views Thread by McA | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.