By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

list unpack trick?

P: n/a
I find that I use some list unpacking construct very often:

name, value = s.split('=',1)

So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as
name='a' and value='b=c'.

The only issue is when s does not contain the character '=', let's say it
is 'xyz', the result list has a len of 1 and the unpacking would fail. Is
there some really handy trick to pack the result list into len of 2 so
that it unpack as name='xyz' and value=''?

So more generally, is there an easy way to pad a list into length of n
with filler items appended at the end?
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, Jan 21, 2005 at 08:02:38PM -0800, aurora wrote:
I find that I use some list unpacking construct very often:

name, value = s.split('=',1)

So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as
name='a' and value='b=c'.

The only issue is when s does not contain the character '=', let's say it
is 'xyz', the result list has a len of 1 and the unpacking would fail. Is
there some really handy trick to pack the result list into len of 2 so
that it unpack as name='xyz' and value=''?


what about:
s.find( '=' )!=-1 and s.split( '=', 1 ) or [s,'']

bye bye - sifu
Jul 18 '05 #2

P: n/a
"aurora" wrote:
The only issue is when s does not contain the character '=', let's say it is 'xyz', the result
list has a len of 1 and the unpacking would fail. Is there some really handy trick to pack the
result list into len of 2 so that it unpack as name='xyz' and value=''?
do you need a trick? spelling it out works just fine:

try:
key, value = field.split("=", 1)
except:
key = field; value = ""

or

if "=" in field:
key, value = field.split("=", 1)
else:
key = field; value = ""

(the former might be slightly more efficient if the "="-less case is uncommon)

or, if you insist:

key, value = re.match("([^=]*)(?:=(.*))?", field).groups()

or

key, value = (field + "="["=" in field:]).split("=", 1)

but that's not really worth the typing. better do

key, value = splitfield(field)

with a reasonable definition of splitfield (I recommend the try-except form).
So more generally, is there an easy way to pad a list into length of n with filler items appended
at the end?


some variants (with varying semantics):

list = (list + n*[item])[:n]

or

list += (n - len(list)) * [item]

or (readable):

if len(list) < n:
list.extend((n - len(list)) * [item])

etc.

</F>

Jul 18 '05 #3

P: n/a
Fredrik Lundh <fr*****@pythonware.com> wrote:
...
or (readable):

if len(list) < n:
list.extend((n - len(list)) * [item])


I find it just as readable without the redundant if guard -- just:

alist.extend((n - len(alist)) * [item])

of course, this guard-less version depends on N*[x] being the empty list
when N<=0, but AFAIK that's always been the case in Python (and has
always struck me as a nicely intuitive semantics for that * operator).

itertools-lovers may prefer:

alist.extend(itertools.repeat(item, n-len(alist)))

a bit less concise but nice in its own way (itertools.repeat gives an
empty iterator when its 2nd argument is <=0, of course).
Alex
Jul 18 '05 #4

P: n/a
Alex Martelli wrote:
or (readable):

if len(list) < n:
list.extend((n - len(list)) * [item])


I find it just as readable without the redundant if guard -- just:

alist.extend((n - len(alist)) * [item])


the guard makes it obvious what's going on, also for a reader that doesn't
know exactly how "*" behaves for negative counts. once you've seen the
"compare length to limit" and "extend", you don't have to parse the rest of
the statement.

</F>

Jul 18 '05 #5

P: n/a
Fredrik Lundh <fr*****@pythonware.com> wrote:
Alex Martelli wrote:
or (readable):

if len(list) < n:
list.extend((n - len(list)) * [item])
I find it just as readable without the redundant if guard -- just:

alist.extend((n - len(alist)) * [item])


the guard makes it obvious what's going on, also for a reader that doesn't
know exactly how "*" behaves for negative counts.


It does, but it's still redundant, like, say,
if x < 0:
x = abs(x)
makes things obvious even for a reader not knowing exactly how abs
behaves for positive arguments. Redundancy in the code to try and
compensate for a reader's lack of Python knowledge is not, IMHO, a
generally very productive strategy. I understand perfectly well that
you and others may disagree, but I just thought it worth stating my
personal opinion in the matter.
once you've seen the
"compare length to limit" and "extend", you don't have to parse the rest of
the statement.


Sorry, I don't get this -- it seems to me that I _do_ still have to
"parse the rest of the statement" to understand exactly what alist is
being extended by.
Alex
Jul 18 '05 #6

P: n/a
Thanks. I'm just trying to see if there is some concise syntax available
without getting into obscurity. As for my purpose Siegmund's suggestion
works quite well.

The few forms you have suggested works. But as they refer to list multiple
times, it need a separate assignment statement like

list = s.split('=',1)

I am think more in the line of string.ljust(). So if we have a
list.ljust(length, filler), we can do something like

name, value = s.split('=',1).ljust(2,'')

I can always break it down into multiple lines. The good thing about list
unpacking is its a really compact and obvious syntax.

On Sat, 22 Jan 2005 08:34:27 +0100, Fredrik Lundh <fr*****@pythonware.com>
wrote:
....
So more generally, is there an easy way to pad a list into length of n
with filler items appended
at the end?


some variants (with varying semantics):

list = (list + n*[item])[:n]

or

list += (n - len(list)) * [item]

or (readable):

if len(list) < n:
list.extend((n - len(list)) * [item])

etc.

</F>

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.