default value in a list
Paul McGuire
ptmcg at austin.rr._bogus_.com
Fri Jan 21 18:42:16 EST 2005
"TB" <tbolands at yahoo.com> wrote in message
news:1106349263.943972.72710 at f14g2000cwb.googlegroups.com...
> Hi,
>
> Is there an elegant way to assign to a list from a list of unknown
> size? For example, how could you do something like:
>
> >>> a, b, c = (line.split(':'))
> if line could have less than three fields?
>
> Thanks,
> TB
>
I asked a very similar question a few weeks ago, and from the various
suggestions, I came up with this:
line = "AAAA:BBB"
expand = lambda lst,default,minlen : (lst + [default]*minlen)[0:minlen]
a,b,c = expand( line.split(":"), "", 3 )
print a
print b
print c
-- Paul
More information about the Python-list
mailing list