ValueError vs IndexError, unpacking arguments with string.split

Chris Angelico rosuav at gmail.com
Fri Nov 30 18:23:05 EST 2018


On Sat, Dec 1, 2018 at 10:18 AM Morten W. Petersen <morphex at gmail.com> wrote:
>
> On Fri, Nov 30, 2018 at 7:25 PM Dan Sommers <
> 2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>
> > On 11/30/18 12:00 PM, Morten W. Petersen wrote:
> >
> >  > I guess syntax could be added, so that
> >  >
> >  > a, b, @c = some sequence
> >  >
> >  > would initialize a and b, and leave anything remaining in c.  We could
> >  > then call this @ syntax "teh snek".
> >
> > Close.  ;-)  Try this:
> >
> >      a, b, *c = [4, 5, 6, 7]
> >
>
>
>
> I did not know that.  Or maybe I read it somewhere and it was in the
> subconscious.
>

Be aware that this isn't the same as "leaving" everything in c. It
will specifically gather everything in the rest of the iterable and
put it all into a list in c. For this example, that wouldn't be a
problem, but consider:

>>> x = range(10)
>>> a, b, *c = x
>>> print(x)
range(0, 10)
>>> print(a, b, c)
0 1 [2, 3, 4, 5, 6, 7, 8, 9]

But as long as you never dive into infinite iterators, it's going to be fine.

ChrisA



More information about the Python-list mailing list