[Python-ideas] list / array comprehensions extension
Devin Jeanpierre
jeanpierreda at gmail.com
Thu Dec 15 18:53:33 CET 2011
This would behave differently than function calls! I can't call foo(a, b, *x, c)
I'm actually +1 on unpacking in literals, though.
-- Devin
On Thu, Dec 15, 2011 at 12:35 PM, 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.
>
> -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
More information about the Python-ideas
mailing list