[Python-ideas] Star assignment in iterator way?

Terry Reedy tjreedy at udel.edu
Tue Nov 21 13:13:01 EST 2017


On 11/21/2017 3:54 AM, Kirill Balunov wrote:
> Hello. Currently during star assignement the new list is created.
> What was the idea to produce new list instead of returning an iterator?
> It seems to me that returning an iterator more suited to the spirit of 
> Python 3.
> There are three cases:
> 
> 1. a,b,c,*d = something_iterable
> 2. *a,b,c,d = something_iterable
> 3. a,*b,c,d = something_iterable
> 
> The first one is obvious.

Right, and easily dealt with with current Python.

d = iter(something iterable)
a,b,c = islice(d, 3)  (or 3 next(d) calls)

More typical is to pull one item off the iterator with next(), as is 
optionally done with csv readers.

it = iter(iterable)
header = next(it)  # such as column names
for item in it: process(item)


> For the rest two we always need to iterate 
> through
> entire iterable to achieve values for b,c,d (or c,d) binding. But this 
> can be done
> more memory effiecient than currently (may be I'm wrong).

For the general case, there is no choice but to save in a list.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list