default value in a list
nick at craig-wood.com
Sat Jan 22 05:30:22 EST 2005
TB <tbolands at yahoo.com> wrote:
> 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?
You could use this old trick...
a, b, c = (line+"::").split(':')[:3]
Or this version if you want something other than "" as the default
a, b, b = (line.split(':') + 3*[None])[:3]
BTW This is a feature I miss from perl...
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list