# [Python-ideas] list / array comprehensions extension

Yuval Greenfield ubershmekel at gmail.com
Thu Dec 15 18:30:57 CET 2011

```On Thu, Dec 15, 2011 at 6:26 PM, Alexander Heger <python at 2sn.net> wrote:

> Hi,
>
> in IDL/GDL, if at have an array
> x = [1,2,3]
> ?I can simply construct a new array by
> y = [0,x]
> which would give
> [0,1,2,3]
>
> I know you can do this case with
> x[0:0] = [0]
> but obviously more complicated cases are conceivable, common for me, in
> fact
> x=[1,2,3]
> y=[5,6,7]
> z=[0,x,4,y,8]
> result
> [0,1,2,3,4,5,6,7,8]
>
> or maybe even
> z=[0,x[0:2],5]
> and so forth.
>
> On the other hand, if you have a function you can pass the list of
> arguments to another function
> def f(*args):
> ? g(*args)
> which expands the list args. ?So, would it be possible - not even
> reasonable to have something similar for list and arrays, maybe
> dictionaries, in comprehensions as well?
>
> x = [1,2,3]
> y = [0,*x]
> to obtain [0,1,2,3]
>
> or similar for lists
> x = (1,2,3)
> y = (0,*x)
> to obtain (0,1,2,3)
>
> (for dictionaries, which are not sorted, the extend function seems fine)
>
> Or is there a way of doing this that in a similarly compact and
> obvious way I did not yet discover?
>
> -Alexander
> ______________________________**_________________
> 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>
>

This is the wrong list for such a question. Python-ideas is a place to
discuss ideas for the python language itself. You can use
stackoverflow.comin the future.

In any case, is this good enough?

>>> x = [1,2,3]
>>> y = [0] + x
>>> y
[0, 1, 2, 3]

Yuval Greenfield
-------------- next part --------------
An HTML attachment was scrubbed...