[Python-ideas] list / array comprehensions extension

Alexander Heger python at 2sn.net
Thu Dec 15 17:26:50 CET 2011


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



More information about the Python-ideas mailing list