[Python-ideas] Re: Deprecate sum of lists

2021-06-22 Thread Neil Girdhar
As someone who originally worked on implementing PEP 448, I wanted to chime in that while I agreed with the decision to be conservative when adding new features, I originally hoped that [*x for x in xs] would eventually make its way into Python. To me, it seemed intuitive and I hoped people

[Python-ideas] Re: Deprecate sum of lists

2021-06-20 Thread Christopher Barker
Flatten is used in numpy: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html?highlight=flatten#numpy.ndarray.flatten Where it is a method, and goes all the way to one dimension. A builtin flatten can’t be the same, but it would be nice if it weren’t too different. e.g.

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Marc-Andre Lemburg
On 19.06.2021 17:17, Serhiy Storchaka wrote: > 19.06.21 18:12, Marc-Andre Lemburg пише: >>> But there could be endless debate about whether flatten( ("x", "y") ) should >>> return a list or a tuple... >> >> Have it return an iterator :-) > > flatten = itertools.chain.from_iterable Well, like I

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Serhiy Storchaka
19.06.21 18:12, Marc-Andre Lemburg пише: >> But there could be endless debate about whether flatten( ("x", "y") ) should >> return a list or a tuple... > > Have it return an iterator :-) flatten = itertools.chain.from_iterable ___ Python-ideas mailing

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Marc-Andre Lemburg
On 19.06.2021 17:03, Guido van Rossum wrote: > I think I would find flatten(a) more readable than [*chunk for chunk in a], > and > more discoverable: this operation is called "flatten" in other languages, so > users are going to search the docs or help for that. +1 > But there could be endless

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Guido van Rossum
On Fri, Jun 18, 2021 at 10:15 PM Steven D'Aprano wrote: > On Fri, Jun 18, 2021 at 09:33:49PM -0700, Guido van Rossum wrote: > > > Now, this shouldn't be considered an airtight argument against [*chunk > for > > ...], but it does show that there's no straightforward explanation of its > > meaning

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Ricky Teachey
On Sat, Jun 19, 2021, 5:00 AM Steven D'Aprano wrote: Why are we arguing about `[x,y for y in a]` when nobody has requested that syntax? -- Steve I've wanted many times to be able to write the star unpack there, even as a relatively modestly experienced python user. But it has never occurred

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Serhiy Storchaka
19.06.21 07:33, Guido van Rossum пише: > [1] Does the unary star operator have a name when used like this in > Python? In JavaScript the equivalent syntax ("...chunk", where the "..." > are a literal ellipsis) is called "spread". We could borrow this term. I would not call is an operator.

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Serhiy Storchaka
18.06.21 18:42, Mathew Elman пише: > I don't see how allowing > > [x, y for x in a] > > follows from allowing > > [*chunk for chunk in list_of_lists]. Because in all other contexts you can replace `*(x, y)` with `x, y`. f(*(x, y)) => f(x, y) [*(x, y)] => [x, y] {*(x, y)} => {x,

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Steven D'Aprano
Why are we arguing about `[x,y for y in a]` when nobody has requested that syntax? -- Steve ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Serhiy Storchaka
18.06.21 17:38, Guido van Rossum пише: > Note the ambiguity around whether the user might have meant > >     [x,(y for y in a)] > > or > >     [(x, y) for y in a] Yes, I think that it could be interpreted in one of the following ways: [x, (y for y in a)] [x, *(y for y in a)]

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Chris Angelico
On Sat, Jun 19, 2021 at 5:13 PM Rob Cliffe via Python-ideas wrote: > > > > On 19/06/2021 05:33, Guido van Rossum wrote: > > On Fri, Jun 18, 2021 at 8:40 PM Steven D'Aprano wrote: >> >> On Fri, Jun 18, 2021 at 07:38:49AM -0700, Guido van Rossum wrote: >> >> > Note the ambiguity around whether the

[Python-ideas] Re: Deprecate sum of lists

2021-06-19 Thread Rob Cliffe via Python-ideas
On 19/06/2021 05:33, Guido van Rossum wrote: On Fri, Jun 18, 2021 at 8:40 PM Steven D'Aprano > wrote: On Fri, Jun 18, 2021 at 07:38:49AM -0700, Guido van Rossum wrote: > Note the ambiguity around whether the user might have meant > >     [x,(y for

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Steven D'Aprano
On Fri, Jun 18, 2021 at 09:33:49PM -0700, Guido van Rossum wrote: > Now, this shouldn't be considered an airtight argument against [*chunk for > ...], but it does show that there's no straightforward explanation of its > meaning through equivalence (like the OP seemed to think), and I think this

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Guido van Rossum
On Fri, Jun 18, 2021 at 8:40 PM Steven D'Aprano wrote: > On Fri, Jun 18, 2021 at 07:38:49AM -0700, Guido van Rossum wrote: > > > Note the ambiguity around whether the user might have meant > > > > [x,(y for y in a)] > > > > or > > > > [(x, y) for y in a] > > We already have a rule to

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Steven D'Aprano
On Fri, Jun 18, 2021 at 07:38:49AM -0700, Guido van Rossum wrote: > Note the ambiguity around whether the user might have meant > > [x,(y for y in a)] > > or > > [(x, y) for y in a] We already have a rule to disambiguate generator comprehensions: they must always be parenthesized

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Steven D'Aprano
On Fri, Jun 18, 2021 at 10:10:33PM +0900, Stephen J. Turnbull wrote: > Pretty clearly the * means ... > > It did take me a bit of thought to come to Ben's intended > interpretation ... So perhaps not actually that clear then? *wink* In hindsight, after realising what Ben's intention was (at

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Paul Svensson
On Fri, 18 Jun 2021, Serhiy Storchaka wrote: 18.06.21 00:22, Ben Rudiak-Gould пише: Okay, slightly off-topic, but can we *please* allow     [*chunk for chunk in list_of_lists] some day. I think it was left out because some discussion concluded it would be too confusing, which is ridiculous.

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Rob Cliffe via Python-ideas
On 18/06/2021 16:42, Mathew Elman wrote: I don't see how allowing [x, y for x in a] follows from allowing [*chunk for chunk in list_of_lists]. Nor do I. Rob Cliffe ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Damian Shaw
Is there somewhere where you can use "*ab" but not "a ,b" currently in Python? On Fri, Jun 18, 2021 at 11:43 AM Mathew Elman wrote: > I don't see how allowing > > [x, y for x in a] > > follows from allowing > > [*chunk for chunk in list_of_lists]. >

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Mathew Elman
I don't see how allowing [x, y for x in a] follows from allowing [*chunk for chunk in list_of_lists]. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Guido van Rossum
On Fri, Jun 18, 2021 at 00:43 Serhiy Storchaka wrote: > 18.06.21 00:22, Ben Rudiak-Gould пише: > > Okay, slightly off-topic, but can we *please* allow > > > > [*chunk for chunk in list_of_lists] > > > > some day. I think it was left out because some discussion concluded it > > would be too

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Chris Angelico
On Fri, Jun 18, 2021 at 11:13 PM Stephen J. Turnbull wrote: > Serhiy writes that a reason for not allowing this is that you'd want > to allow [x, y for x in l], splicing the x, y values into the result > list. That doesn't make sense to me, for two reasons. x, y already > has a meaning in that

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Stephen J. Turnbull
David Mertz writes: > On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould > > > Okay, slightly off-topic, but can we *please* allow > > > > [*chunk for chunk in list_of_lists] > > > > It is completely non-obvious to me what that would even MEAN. I cannot > derive anything obvious from

[Python-ideas] Re: Deprecate sum of lists

2021-06-18 Thread Serhiy Storchaka
18.06.21 00:22, Ben Rudiak-Gould пише: > Okay, slightly off-topic, but can we *please* allow > >     [*chunk for chunk in list_of_lists] > > some day. I think it was left out because some discussion concluded it > would be too confusing, which is ridiculous. I assumed it would work and > was

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Guido van Rossum
Wow, strong language. Not really helping people see it your way. On Thu, Jun 17, 2021 at 14:26 Ben Rudiak-Gould wrote: > On Thu, Jun 17, 2021 at 12:37 AM Serhiy Storchaka > wrote: > >> And it is equivalent to pure Python code >> >> [x for chunk in list_of_lists for x in chunk] >> > > Okay,

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Ben Rudiak-Gould
On Thu, Jun 17, 2021 at 3:09 PM Steven D'Aprano wrote: > On Thu, Jun 17, 2021 at 02:22:29PM -0700, Ben Rudiak-Gould wrote: > > [*chunk for chunk in list_of_lists] > > What would that do? The difference between chunk and *chunk in the expression of a list comprehension would be the same as

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Jelle Zijlstra
El jue, 17 jun 2021 a las 15:26, Steven D'Aprano () escribió: > On Thu, Jun 17, 2021 at 02:51:44PM -0700, Jelle Zijlstra wrote: > > El jue, 17 jun 2021 a las 14:45, David Mertz () > escribió: > > > > > On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould > > > > > >> Okay, slightly off-topic, but can

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Steven D'Aprano
On Thu, Jun 17, 2021 at 02:51:44PM -0700, Jelle Zijlstra wrote: > El jue, 17 jun 2021 a las 14:45, David Mertz () escribió: > > > On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould > > > >> Okay, slightly off-topic, but can we *please* allow > >> > >> [*chunk for chunk in list_of_lists] > My

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Steven D'Aprano
On Thu, Jun 17, 2021 at 02:22:29PM -0700, Ben Rudiak-Gould wrote: > Okay, slightly off-topic, but can we *please* allow > > [*chunk for chunk in list_of_lists] What would that do? The only thing I can guess it would do is the equivalent of: result = [] for chunk in list_of_lists:

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread David Mertz
On Thu, Jun 17, 2021 at 5:52 PM Jelle Zijlstra wrote: > El jue, 17 jun 2021 a las 14:45, David Mertz () escribió: > >> On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould >> >>> Okay, slightly off-topic, but can we *please* allow >>> >>> [*chunk for chunk in list_of_lists] >>> >> > It is

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Jelle Zijlstra
El jue, 17 jun 2021 a las 14:45, David Mertz () escribió: > On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould > >> Okay, slightly off-topic, but can we *please* allow >> >> [*chunk for chunk in list_of_lists] >> > > It is completely non-obvious to me what that would even MEAN. I cannot > derive

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread David Mertz
On Thu, Jun 17, 2021, 5:24 PM Ben Rudiak-Gould > Okay, slightly off-topic, but can we *please* allow > > [*chunk for chunk in list_of_lists] > It is completely non-obvious to me what that would even MEAN. I cannot derive anything obvious from other uses of *. If I had to guess, I'd think

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Ben Rudiak-Gould
On Thu, Jun 17, 2021 at 12:37 AM Serhiy Storchaka wrote: > And it is equivalent to pure Python code > > [x for chunk in list_of_lists for x in chunk] > Okay, slightly off-topic, but can we *please* allow [*chunk for chunk in list_of_lists] some day. I think it was left out because

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Christopher Barker
On Wed, Jun 16, 2021 at 10:24 PM David Mertz wrote: > > If you read the BPO the OP linked, that was a suggested patch to optimize >> sum(list_of_lists) -- I'm not sure that's such a bad idea after all. >> > > The proposal was to drop in .__iadd__() for .__add__(), wasn't it? As a > heavy NumPy

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Chris Angelico
On Fri, Jun 18, 2021 at 3:43 AM Stephen J. Turnbull wrote: > > Chris Angelico writes: > > > Insufficiently trolly - lacks walrus operator. :) > > > > >>> l = [[1,2],[3,4],[5,6],[7,8],[9]] > > >>> any(map((x:=[]).extend, l)) or x > > [1, 2, 3, 4, 5, 6, 7, 8, 9] > > > > ChrisA > > This you?

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Stephen J. Turnbull
Chris Angelico writes: > Insufficiently trolly - lacks walrus operator. :) > > >>> l = [[1,2],[3,4],[5,6],[7,8],[9]] > >>> any(map((x:=[]).extend, l)) or x > [1, 2, 3, 4, 5, 6, 7, 8, 9] > > ChrisA This you? https://mobile.twitter.com/Tr0llyTr0llFace/photo :-)

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Oliver Margetts
Arbitrary and complex nested structures do seem like they would require a complex solution. OTOH `more_itertools.flatten` seems ergonomic - and it is very simple, just a wrapper around `itertools.chain.from_iterable` with a memorable name. If that's the preferred solution, nudging users in the

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Steven D'Aprano
On Wed, Jun 16, 2021 at 10:13:40PM -0700, Christopher Barker wrote: > If, as suggested, flattening a list of lists is a common operation, a nice > clean and efficient built in way to do it would be reasonable. Heck, you > could make it a list method :-) People have been talking about a flatten

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Steven D'Aprano
On Wed, Jun 16, 2021 at 04:01:24PM -0700, Christopher Barker wrote: > I’m pretty sure that using sum with strings was a real issue in real code > before it was disallowed. But not with the sum() function, which has prohibited strings from it's introduction in 2.3 :-)

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Serhiy Storchaka
17.06.21 06:03, David Mertz пише: > I'm sympathetic to raising an exception on `sum(list_of_lists)` similar > to `sum(list_of_strings)`.  But what exactly is the recommended > substitute?  We have this: > > list(chain.from_iterable(list_of_lists)) > > Which is pretty nice, and what I'd probably

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Chris Angelico
On Thu, Jun 17, 2021 at 4:07 PM Guido van Rossum wrote: > > Just trolling along, flattening a list could be written as > > functools.reduce(list.__iadd__, xs, []) > > Right? > Insufficiently trolly - lacks walrus operator. :) >>> l = [[1,2],[3,4],[5,6],[7,8],[9]] >>> any(map((x:=[]).extend, l))

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread David Mertz
Still an import for that version! On Thu, Jun 17, 2021, 2:07 AM Guido van Rossum wrote: > Just trolling along, flattening a list could be written as > > functools.reduce(list.__iadd__, xs, []) > > Right? > > On Wed, Jun 16, 2021 at 22:53 David Mertz wrote: > >> On Thu, Jun 17, 2021, 1:38 AM

[Python-ideas] Re: Deprecate sum of lists

2021-06-17 Thread Guido van Rossum
Just trolling along, flattening a list could be written as functools.reduce(list.__iadd__, xs, []) Right? On Wed, Jun 16, 2021 at 22:53 David Mertz wrote: > On Thu, Jun 17, 2021, 1:38 AM Chris Angelico > >> >>> list(chain.from_iterable(list_of_lists)) >> >> > More-itertools has flatten(): >>

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread David Mertz
On Thu, Jun 17, 2021, 1:38 AM Chris Angelico > >>> list(chain.from_iterable(list_of_lists)) > > > More-itertools has flatten(): > https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.flatten. > That seems better than a method specific to lists. > > ... which is built on top of

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Chris Angelico
On Thu, Jun 17, 2021 at 3:27 PM David Mertz wrote: > > On Thu, Jun 17, 2021, 1:14 AM Christopher Barker >>> >>> I'm sympathetic to raising an exception on `sum(list_of_lists)` similar to >>> `sum(list_of_strings)`. But what exactly is the recommended substitute? >>> We have this: >>> >>>

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread David Mertz
On Thu, Jun 17, 2021, 1:14 AM Christopher Barker > I'm sympathetic to raising an exception on `sum(list_of_lists)` similar to >> `sum(list_of_strings)`. But what exactly is the recommended substitute? >> We have this: >> >> list(chain.from_iterable(list_of_lists)) >> > > If you read the BPO the

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Christopher Barker
On Wed, Jun 16, 2021 at 8:04 PM David Mertz wrote: > Flattening nested lists is not uncommon. And yes sum seemed the natural >> way to do it at the time. >> > > I remember the discussion in 2013 about the quadratic performance of > summing strings. There were proposals for optimization of the

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread David Mertz
On Wed, Jun 16, 2021 at 7:35 PM Oliver Margetts wrote: > I came across this just processing JSON data - flattening a list of > 'deals' into a list of individual 'trades'. Flattening nested lists is not > uncommon. And yes sum seemed the natural way to do it at the time. > I remember the

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread MRAB
On 2021-06-17 00:57, Chris Angelico wrote: On Thu, Jun 17, 2021 at 9:46 AM Oliver Margetts wrote: > I'm not sure why it doesn't special-case it to "".join() One reason might be because you'd have to read the entire iterator to know if it really was only strings. So there are concerns with

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Chris Angelico
On Thu, Jun 17, 2021 at 9:46 AM Oliver Margetts wrote: > > > I'm not sure why it doesn't special-case it to "".join() > One reason might be because you'd have to read the entire iterator to know if > it really was only strings. So there are concerns with generators which > complicate things a

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Oliver Margetts
> I'm not sure why it doesn't special-case it to "".join() One reason might be because you'd have to read the entire iterator to know if it really was only strings. So there are concerns with generators which complicate things a fair bit On Thu, 17 Jun 2021, 12:12 am Chris Angelico, wrote: > On

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Oliver Margetts
I came across this just processing JSON data - flattening a list of 'deals' into a list of individual 'trades'. Flattening nested lists is not uncommon. And yes sum seemed the natural way to do it at the time. Yes, you're right some people might find it convenient and use it in the REPL. It just

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Chris Angelico
On Thu, Jun 17, 2021 at 9:04 AM Christopher Barker wrote: > > > >> >> The performance error for strings should be considered an anomaly, not a >> feature to be extended to anything that could be used, or misused, with >> non-linear behaviour. > > > I’m pretty sure that using sum with strings was

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Christopher Barker
> > The performance error for strings should be considered an anomaly, not a > feature to be extended to anything that could be used, or misused, with > non-linear behaviour. I’m pretty sure that using sum with strings was a real issue in real code before it was disallowed. But the irony is

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Steven D'Aprano
On Wed, Jun 16, 2021 at 02:50:06PM +, Oliver Margetts wrote: > Hi all, I came across the following performance issue with the sum function > while summing lists: https://bugs.python.org/issue18305 It's been discussed > previously in this list and other issues. Did you actually use sum to

[Python-ideas] Re: Deprecate sum of lists

2021-06-16 Thread Rob Cliffe via Python-ideas
This seems sensible to me.  I remember Guido vetoing allowing random.choice() to accept a set, and for the same reason - it encourages inefficient code. Rob Cliffe On 16/06/2021 15:50, Oliver Margetts wrote: Hi all, I came across the following performance issue with the sum function while