Re: [Python-ideas] Order of loops in list comprehension
C Anthony Risinger wrote: Erlang/Elixir (sorry after 6 years python this is what I do now!) does it the same way as python: >>> [{X, Y} || X <- [1,2,3], Y <- [a,b]]. [{1,a},{1,b},{2,a},{2,b},{3,a},{3,b}] Here X is the outer loop. I think the confusion stems from doing it both ways at the same time. If the semicolon syntax I suggested were available, you'd be able to choose either order, and maybe even mix them in the one comprehension. Not sure if that's a good thing or not... -- Greg ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
On Oct 22, 2016 2:51 AM, "Alexander Heger" wrote: >>> >>> For me the current behaviour does not seem unreasonable as it resembles the order in which you write out loops outside a comprehension >>> >>> >>> That's true, but the main reason for having comprehensions >>> syntax in the first place is so that it can be read >>> declaratively -- as a description of the list you want, >>> rather than a step-by-step sequence of instructions for >>> building it up. >>> >>> If you have to stop and mentally transform it into nested >>> for-statements, that very purpose is undermined. >> >> Exactly. > > > Well, an argument that was often brought up on this forum is that Python should do things consistently, and not in one way in one place and in another way in another place, for the same thing. Here it is about the order of loop execution. The current behaviour in comprehension is that is ts being done the same way as in nested for loops. Which is easy enough to remember. Same way, everywhere. A strict interpretation by this logic would also require the [x ...] part to be at the end, like [... x] since that's how it would look in a nested for loop (inside deepest loop). I personally agree with what many others have said, in that comprehension order is not intuitive as is. I still page fault about it after many years of using. Is there a way to move the expression bit to the end in a backcompat way? It might be a completely different syntax though (I think both colons and semicolons were suggested). FWIW, Erlang/Elixir (sorry after 6 years python this is what I do now!) does it the same way as python: >>> [{X, Y} || X <- [1,2,3], Y <- [a,b]]. [{1,a},{1,b},{2,a},{2,b},{3,a},{3,b}] Here X is the outer loop. I think the confusion stems from doing it both ways at the same time. We retain the for loop order but then hoist the expression to the top. Ideally we'd either not do that, or reverse the for loop order. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
On 22.10.2016 09:50, Alexander Heger wrote: Well, an argument that was often brought up on this forum is that Python should do things consistently, and not in one way in one place and in another way in another place, for the same thing. Like * in list displays? ;-) Here it is about the order of loop execution. The current behaviour in comprehension is that is ts being done the same way as in nested for loops. It still would. Just read it from right to left. The order stays the same. Which is easy enough to remember. Same way, everywhere. I am sorry but many disagree with you on this thread. I still don't understand why the order needs to be one-way anyway. Comprehensions are a declarative construct, so it should be possible to mix those "for .. in .."s up in an arbitrary order. Cheers, Sven ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
> > >> For me the current behaviour does not seem unreasonable as it resembles >>> the order in which you write out loops outside a comprehension >>> >> >> That's true, but the main reason for having comprehensions >> syntax in the first place is so that it can be read >> declaratively -- as a description of the list you want, >> rather than a step-by-step sequence of instructions for >> building it up. >> >> If you have to stop and mentally transform it into nested >> for-statements, that very purpose is undermined. >> > Exactly. Well, an argument that was often brought up on this forum is that Python should do things consistently, and not in one way in one place and in another way in another place, for the same thing. Here it is about the order of loop execution. The current behaviour in comprehension is that is ts being done the same way as in nested for loops. Which is easy enough to remember. Same way, everywhere. -Alexander ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
On 21.10.2016 01:43, Greg Ewing wrote: Alexander Heger wrote: For me the current behaviour does not seem unreasonable as it resembles the order in which you write out loops outside a comprehension That's true, but the main reason for having comprehensions syntax in the first place is so that it can be read declaratively -- as a description of the list you want, rather than a step-by-step sequence of instructions for building it up. If you have to stop and mentally transform it into nested for-statements, that very purpose is undermined. Exactly. Sven ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
Alexander Heger wrote: For me the current behaviour does not seem unreasonable as it resembles the order in which you write out loops outside a comprehension That's true, but the main reason for having comprehensions syntax in the first place is so that it can be read declaratively -- as a description of the list you want, rather than a step-by-step sequence of instructions for building it up. If you have to stop and mentally transform it into nested for-statements, that very purpose is undermined. -- Greg ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
For me the current behaviour does not seem unreasonable as it resembles the order in which you write out loops outside a comprehension except that the expression for generated values is provided first. On 21 October 2016 at 05:03, Sven R. Kunze wrote: > On 19.10.2016 00:08, Rob Cliffe wrote: > >> >> But it's far too late to change it now, sadly. >>> >> Indeed. :-( But if I were ruler of the world and could have my own >> wish-list for Python 4, this (as per the first example) would be on it. >> > > I don't see no reason why we can't make it. > > Personally, I also dislike this behavior. > > > Cheers, > Sven > > ___ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
On 19.10.2016 00:08, Rob Cliffe wrote: But it's far too late to change it now, sadly. Indeed. :-( But if I were ruler of the world and could have my own wish-list for Python 4, this (as per the first example) would be on it. I don't see no reason why we can't make it. Personally, I also dislike this behavior. Cheers, Sven ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Re: [Python-ideas] Order of loops in list comprehension
On 18/10/2016 07:40, Greg Ewing wrote: Random832 wrote: For me, it's the fact that: [[a for a in b] for b in ['uvw', 'xyz']] == [['u', 'v', 'w'], ['x', 'y', 'z']] which makes me want to write: [a for a in b for b in ['uvw', 'xyz']] You're not alone! Lately I've been becoming convinced that this is the way we should have done it right back at the beginning. Me too. When I first got to grips with the order of loops in a list comprehension, I found it counter-intuitive and jarring. I still have to remind myself each time. I guess different people have different mental models, and may feel differently. The best way I can think of to explain why I feel this way is: If the syntax were [ x for x in alist for alist in list-of-lists ] there is a smooth (or rather, monotonic) gradation from the smallest object (x) to the next biggest object (alist) to the biggest object (list-of-lists), which IMHO is easier to follow. Each object is conceptually zero or one steps from its neighbour. But with the actual syntax [ x for alist in list-of-lists for x in alist ] there is a conceptual hiatus after "x" ("what on earth are alist and list-of-lists, and what have they got do to with x?"). This would be even more marked with more than 2 loops: we jump from the lowest level object to the two highest level objects, and it all seems like a disorienting non-sequitur until the very last loop "joins the dots". You have to identify the "x" at the beginning with the "x" near (but not at!) the end. Instead of (ideally, if not always in practice) reading the expression from left-to-right in one go, your eyes are forced to jump around in order for your brain to assimilate it. A possible alternative syntax might be to follow more closely the for-loop syntax, e.g. [ for alist in list-of-lists: for x in alist: x ] Here the "conceptual jump" between each object and the next is either 1 or 2, which for me makes this a "second best" option. But at least the "conceptual jump" is bounded (regardless of the number of loops), and this syntax has the advantage of familiarity. But it's far too late to change it now, sadly. Indeed. :-( But if I were ruler of the world and could have my own wish-list for Python 4, this (as per the first example) would be on it. Best wishes Rob Cliffe ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/