[Python-ideas] list / array comprehensions extension

Michael Foord fuzzyman at gmail.com
Thu Dec 15 18:48:28 CET 2011


On 15 December 2011 17:35, Alexander Heger <python at 2sn.net> wrote:

> 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.
>


I quite like that (suggested) syntax.

Michael



>
> -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]
>>
>>
>>  ______________________________**_________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>



-- 

http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111215/385ba0e9/attachment.html>


More information about the Python-ideas mailing list