[Python-ideas] Unpack of sequences

Terry Reedy tjreedy at udel.edu
Fri Aug 31 03:48:48 CEST 2012

On 8/30/2012 8:17 PM, Greg Ewing wrote:
> Mike Graham wrote:
>> That being said, `a, b, c, *_ = d` or similar is probably better than
>> introducing a new way.
> It's inefficient, though, because it results in iterating
> over the remainder of the sequence and building a new sequence
> that will never be used.
> There's currently no way to spell the most efficient way of
> doing this, which is simply to stop iterating and ignore the
> rest of the sequence. There's also no way to unpack the
> head of an infinite sequence without slicing it first.

dit = iter(d)
a,b,d = next(dit), next(dit), next(dit)

or, most efficiently
a = next(dit)
b = next(dit)
c = next(dit)

a, b, c = [next(dit) for i in range(3)]

Terry Jan Reedy

More information about the Python-ideas mailing list