list / array comprehensions extension

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

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]

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 15 December 2011 17:35, Alexander Heger <python@2sn.net> wrote:
I quite like that (suggested) syntax. Michael
-- 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

On 15 December 2011 17:53, Steven D'Aprano <steve@pearwood.info> wrote:
Well, the new tuple unpacking syntax makes it easy to work with the head and tail of a sequence: head, *rest = some_sequence *rest, tail = some_sequence This syntax provides a corollary: some_sequence = (head, *rest) some_sequence = (*rest, tail) Tuple unpacking like this being the equivalent of car and cdr, with the corollary being the equivalent of cons. (Given my limited understanding of these operations.) Michael
-- 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

On 2011-12-15, at 19:02 , Michael Foord wrote:
>>> a, b, *c, d, e = range(15) >>> a 0 >>> b 1 >>> d 13 >>> e 14 >>> c [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Alexander's syntax would be nicely parallel to this. On the other hand, much as Steven I'm not sure how useful it is, especially for tuples.

On 2011-12-15, at 18:35 , Alexander Heger wrote:
I know all of these, but 1) it is not as elegant or short
Sure, I was just answering the rest of of the question
2) why does unpacking not work syntactically the same as for the function parameters?
Because nobody's thought of it so far.
Or is there a way of doing this that in a similarly compact and obvious way I did not yet discover?

On Thu, Dec 15, 2011 at 6:26 PM, Alexander Heger <python@2sn.net> wrote:
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?
Yuval Greenfield

On Thu, Dec 15, 2011 at 07:30:57PM +0200, Yuval Greenfield wrote:
This is the wrong list for such a question. Python-ideas is a place to discuss ideas for the python language itself.
IMO it's ok because the OP proposed a new feature:
On Thu, Dec 15, 2011 at 6:26 PM, Alexander Heger <python@2sn.net> wrote:
y = (0,*x)
and we're going to discuss why it will never happens. Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

On Thu, Dec 15, 2011 at 10:26:50AM -0600, Alexander Heger wrote:
y = [0] + x
y = (0,) + x Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Dear Oleg, OK, these work for the examples I listed, I had not tried that. Thanks! I still think the element extraction would be nice, though. what does not work your way is x = [1,2,3] y = (0,) + x but y = (0,*x) could do ... ? -Alexander On 12/15/2011 11:33 AM, Oleg Broytman wrote:

On 15 December 2011 17:41, Alexander Heger <python@2sn.net> wrote:
In fact there's already an issue for this, with patch: http://bugs.python.org/issue2292 Actually it goes a lot further than just the syntax you suggest... It extends tuple unpacking into a few more places as well. All the best, Michael Foord
-- 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

On 15 December 2011 23:13, Paul Moore <p.f.moore@gmail.com> wrote:
Guido was -0 in the email linked to from the issue. Not sure if that counts as approval... Michael
Paul.
-- 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

On Fri, Dec 16, 2011 at 9:33 AM, Guido van Rossum <guido@python.org> wrote:
I think that -0 was contextual (too many moving parts for the original Py3k release). Today I am +1.
I stand corrected :) (And after skimming #2292, I'm also +1 on the course of action outlined there - the idea is sound in principle, but needs a PEP to thrash out all the niggling little details) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

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]

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 15 December 2011 17:35, Alexander Heger <python@2sn.net> wrote:
I quite like that (suggested) syntax. Michael
-- 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

On 15 December 2011 17:53, Steven D'Aprano <steve@pearwood.info> wrote:
Well, the new tuple unpacking syntax makes it easy to work with the head and tail of a sequence: head, *rest = some_sequence *rest, tail = some_sequence This syntax provides a corollary: some_sequence = (head, *rest) some_sequence = (*rest, tail) Tuple unpacking like this being the equivalent of car and cdr, with the corollary being the equivalent of cons. (Given my limited understanding of these operations.) Michael
-- 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

On 2011-12-15, at 19:02 , Michael Foord wrote:
>>> a, b, *c, d, e = range(15) >>> a 0 >>> b 1 >>> d 13 >>> e 14 >>> c [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] Alexander's syntax would be nicely parallel to this. On the other hand, much as Steven I'm not sure how useful it is, especially for tuples.

On 2011-12-15, at 18:35 , Alexander Heger wrote:
I know all of these, but 1) it is not as elegant or short
Sure, I was just answering the rest of of the question
2) why does unpacking not work syntactically the same as for the function parameters?
Because nobody's thought of it so far.
Or is there a way of doing this that in a similarly compact and obvious way I did not yet discover?

On Thu, Dec 15, 2011 at 6:26 PM, Alexander Heger <python@2sn.net> wrote:
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?
Yuval Greenfield

On Thu, Dec 15, 2011 at 07:30:57PM +0200, Yuval Greenfield wrote:
This is the wrong list for such a question. Python-ideas is a place to discuss ideas for the python language itself.
IMO it's ok because the OP proposed a new feature:
On Thu, Dec 15, 2011 at 6:26 PM, Alexander Heger <python@2sn.net> wrote:
y = (0,*x)
and we're going to discuss why it will never happens. Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

On Thu, Dec 15, 2011 at 10:26:50AM -0600, Alexander Heger wrote:
y = [0] + x
y = (0,) + x Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Dear Oleg, OK, these work for the examples I listed, I had not tried that. Thanks! I still think the element extraction would be nice, though. what does not work your way is x = [1,2,3] y = (0,) + x but y = (0,*x) could do ... ? -Alexander On 12/15/2011 11:33 AM, Oleg Broytman wrote:

On 15 December 2011 17:41, Alexander Heger <python@2sn.net> wrote:
In fact there's already an issue for this, with patch: http://bugs.python.org/issue2292 Actually it goes a lot further than just the syntax you suggest... It extends tuple unpacking into a few more places as well. All the best, Michael Foord
-- 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

On 15 December 2011 23:13, Paul Moore <p.f.moore@gmail.com> wrote:
Guido was -0 in the email linked to from the issue. Not sure if that counts as approval... Michael
Paul.
-- 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

On Fri, Dec 16, 2011 at 9:33 AM, Guido van Rossum <guido@python.org> wrote:
I think that -0 was contextual (too many moving parts for the original Py3k release). Today I am +1.
I stand corrected :) (And after skimming #2292, I'm also +1 on the course of action outlined there - the idea is sound in principle, but needs a PEP to thrash out all the niggling little details) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (12)
-
Alexander Heger
-
Andre Roberge
-
Devin Jeanpierre
-
Ethan Furman
-
Guido van Rossum
-
Masklinn
-
Michael Foord
-
Nick Coghlan
-
Oleg Broytman
-
Paul Moore
-
Steven D'Aprano
-
Yuval Greenfield