list unpack trick?
Siegmund Fuehringer
sifu at isohypse.org
Fri Jan 21 23:18:22 EST 2005
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
More information about the Python-list
mailing list