how best to split into singleton and sequence

George Sakkis gsakkis at rutgers.edu
Tue Oct 18 19:23:55 EDT 2005


"Randy Bush" <randy at psg.com> wrote:

> >>> l = []
> >>> s = 'a|b'
> >>> t, l = s.split('|')
> >>> t
> 'a'
> >>> l
> 'b'
> >>> s = 'a|b|c|d'
> >>> t, l = s.split('|')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: too many values to unpack
> >>>
>
> so, i imagine what is happening is the lhs, t,l, is really
> (t, (l)), i.e. only two items.
>
> so how should i have done this readably and simply?

>>> s = 'a|b|c|d'
>>> l = s.split('|')
>>> t = l.pop(0)

By the way, don't use 'l' as an identifier; it is very close to '1' visually.

George





More information about the Python-list mailing list