list unpack trick?

A

aurora

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?
 
S

Siegmund Fuehringer

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
 
F

Fredrik Lundh

aurora said:
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>
 
A

Alex Martelli

Fredrik Lundh said:
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
 
F

Fredrik Lundh

Alex said:
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>
 
A

Alex Martelli

Fredrik Lundh said:
Alex said:
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
 
A

aurora

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.



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

Nick Coghlan

aurora said:
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,'')

Eh?

Py> s.split('=',1).ljust(2,'')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'ljust'

Cheers,
Nick.
 
F

Fredrik Lundh

Nick said:
Eh?

Py> s.split('=',1).ljust(2,'')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'ljust'

I think the "if we have" was hypothetical.

I still don't see why the OP cannot just write a small helper and be
done with it, but then I don't know anything about how Python is
used, so I might be wrong ;-)

</F>
 
B

Bengt Richter

Eh?

Py> s.split('=',1).ljust(2,'')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'ljust'

Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information. Help on built-in function ljust:

ljust(...)
S.ljust(width[, fillchar]) -> string

Return S left justified in a string of length width. Padding is
done using the specified fill character (default is a space).

I should upgrade too ;-)

Regards,
Bengt Richter
 
B

Bengt Richter

Eh?

Py> s.split('=',1).ljust(2,'')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'ljust'

Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information.Help on built-in function ljust:

ljust(...)
S.ljust(width[, fillchar]) -> string

Return S left justified in a string of length width. Padding is
done using the specified fill character (default is a space).

I should upgrade too ;-)
And learn to read instead of jumping to conclusions ;-/
No ljust for *LIST* objects, D'oh.

Regards,
Bengt Richter
 
A

aurora

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.

Just to clarify the ljust() is a feature wish, probably should be named
something like pad().

Also there is another thread a few hours before this asking about
essentially the same thing.

"default value in a list"
http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/f3affefdb4272270
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top