default value in a list

Steve Holden steve at holdenweb.com
Fri Jan 21 18:36:58 EST 2005


TB wrote:

> 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?
> 
l = line.split(':')

l is a list, whose length will be one more than the number of colons in 
the line.

You can access the elements of the list using a[0], a[2], and so on. For 
example:

  >>> line = "This:is:a:sample:line"
  >>> l = line.split(':')
  >>> l
['This', 'is', 'a', 'sample', 'line']
  >>> for w in l:
  ...   print w
  ...
This
is
a
sample
line
  >>> len(l)
5
  >>>

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list