default value in a list

Nick Craig-Wood nick at
Sat Jan 22 05:30:22 EST 2005

TB <tbolands at> 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> --

More information about the Python-list mailing list