[Python-ideas] list / array comprehensions extension

Alexander Heger python at 2sn.net
Thu Dec 15 18:35:12 CET 2011


Dear Masklinn,

thanks for your suggested solution.

I know all of these, but
1) it is not as elegant or short
2) why does unpacking not work syntactically the same as for the 
function parameters?
It seems a natural extension that appears not to have a syntactic 
conflict.  If it is not even a necessity for consistency.

So, the point is not that something like
[0,*x,0,*y,0]
can't be done in other ways, but that it can't be done in a neat way.

-Alexander

On 12/15/2011 11:27 AM, Masklinn wrote:
> On 2011-12-15, at 17:26 , Alexander Heger wrote:
>>
>> Or is there a way of doing this that in a similarly compact and
>> obvious way I did not yet discover?
>
> If the list is uniform, you can flatten a single level by using `itertools.chain`:
>
>      >>>  import itertools
>      >>>  x = [1,2,3]
>      >>>  y = itertools.chain.from_iterable([[0], x])
>      >>>  list(y)
>      [0, 1, 2, 3]
>      >>>  # alternatively
>      ... y = list(itertools.chain([0], x))
>      >>>  y
>      [0, 1, 2, 3]
>      >>>
>
> I know of no "better" way to do it at the moment, apart from using slice-assignment with a *stop* bound of 0:
>
>      >>>  y = [0, 0]
>      >>>  y[1:0] = x
>      >>>  y
>      [0, 1, 2, 3, 0]
>
>



More information about the Python-ideas mailing list