[Python-ideas] list / array comprehensions extension

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


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

I know you can do this case with
x[0:0] = [0]
but obviously more complicated cases are conceivable, common for me, in fact

or maybe even
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?


More information about the Python-ideas mailing list