Re: zip as iterator and bad/good practices

2015-06-13 Thread Steven D'Aprano
On Sat, 13 Jun 2015 13:48:45 +0100, Oscar Benjamin wrote: > On 13 June 2015 at 08:17, Steven D'Aprano > wrote: >> But an easier way is: >> >> Test = [1, 2] >> Test.extend(Test) >> print(Test) > > I can't see anything in the docs that specify the behaviour that occurs > here. Neither do I, but

Re: zip as iterator and bad/good practices

2015-06-13 Thread Oscar Benjamin
On 13 June 2015 at 08:17, Steven D'Aprano wrote: > On Sat, 13 Jun 2015 13:32:59 +0800, jimages wrote: > >> I am a newbie. I also have been confused when I read the tutorial. It >> recommends make a copy before looping. Then I try. >> #-- >> Test = [1, 2] >> For i in Test: >

Re: zip as iterator and bad/good practices

2015-06-13 Thread Steven D'Aprano
On Sat, 13 Jun 2015 13:32:59 +0800, jimages wrote: > I am a newbie. I also have been confused when I read the tutorial. It > recommends make a copy before looping. Then I try. > #-- > Test = [1, 2] > For i in Test: > Test.append(i) > #-- You don

Re: zip as iterator and bad/good practices

2015-06-12 Thread jimages
> On Jun 12, 2015, at 11:00 PM, Fabien wrote: > but that awful bug made me wonder: is it a bad practice to interactively > modify the list you are iterating over? Yes. I am a newbie. I also have been confused when I read the tutorial. It recommends make a copy before looping. Then I try. #

Re: zip as iterator and bad/good practices

2015-06-12 Thread sohcahtoa82
On Friday, June 12, 2015 at 5:27:21 PM UTC-7, Chris Angelico wrote: > On Sat, Jun 13, 2015 at 10:02 AM, wrote: > >> >>> ints = [0, 1, 2, 2, 1, 4, 6, 5, 5] > >> >>> ints[:] = [i for i in ints if not i % 2] > >> >>> ints > >> [0, 2, 2, 4, 6] > >> > >> > >> -- > >> Terry Jan Reedy > > > > On the

Re: zip as iterator and bad/good practices

2015-06-12 Thread Chris Angelico
On Sat, Jun 13, 2015 at 10:02 AM, wrote: >> >>> ints = [0, 1, 2, 2, 1, 4, 6, 5, 5] >> >>> ints[:] = [i for i in ints if not i % 2] >> >>> ints >> [0, 2, 2, 4, 6] >> >> >> -- >> Terry Jan Reedy > > On the second line of your final solution, is there any reason you're using > `ints[:]` rather t

Re: zip as iterator and bad/good practices

2015-06-12 Thread sohcahtoa82
On Friday, June 12, 2015 at 4:44:08 PM UTC-7, Terry Reedy wrote: > On 6/12/2015 4:34 PM, Laura Creighton wrote: > > The real problem is removing things from lists when you are iterating > > over them, not adding things to the end of lists. > > One needs to iterate backwards. > > >>> ints = [0, 1

Re: zip as iterator and bad/good practices

2015-06-12 Thread Terry Reedy
On 6/12/2015 4:34 PM, Laura Creighton wrote: The real problem is removing things from lists when you are iterating over them, not adding things to the end of lists. One needs to iterate backwards. >>> ints = [0, 1, 2, 2, 1, 4, 6, 5, 5] >>> for i in range(len(ints)-1, -1, -1): if ints[

Re: zip as iterator and bad/good practices

2015-06-12 Thread Terry Reedy
On 6/12/2015 11:00 AM, Fabien wrote: is it a bad practice to interactively modify the list you are iterating over? One needs care. Appending to the end of the list is OK, unless you append a billion items or so ;-) Appending to the end of a queue while *removing* items from the front of the

Re: zip as iterator and bad/good practices

2015-06-12 Thread Laura Creighton
The real problem is removing things from lists when you are iterating over them, not adding things to the end of lists. Python 2.7.9 (default, Mar 1 2015, 12:57:24) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> mylist = [1,2,3] >>> for i in mylis

Re: zip as iterator and bad/good practices

2015-06-12 Thread Mark Lawrence
On 12/06/2015 16:00, Fabien wrote: Folks, I am developing a program which I'd like to be python 2 and 3 compatible. I am still relatively new to python and I use primarily py3 for development. Every once in a while I use a py2 interpreter to see if my tests pass through. I just spent several ho

Re: zip as iterator and bad/good practices

2015-06-12 Thread Fabien
On 06/12/2015 05:26 PM, Ian Kelly wrote: but that awful bug made me wonder: is it a bad practice to >interactively modify the list you are iterating over? Generally speaking, yes, it's bad practice to add or remove items because this may result in items being visited more than once or not at all

Re: zip as iterator and bad/good practices

2015-06-12 Thread Fabien
On 06/12/2015 05:26 PM, Ian Kelly wrote: for stuff, branch in zip(stuffs, branches): > # compute flux > ... > # add to the downstream branch > id_branch = branches.index(branch.flows_to) > branches[id_branch].property.append(stuff_i_computed) Er, I don't s

Re: zip as iterator and bad/good practices

2015-06-12 Thread Ian Kelly
On Fri, Jun 12, 2015 at 9:00 AM, Fabien wrote: > Folks, > > I am developing a program which I'd like to be python 2 and 3 compatible. I > am still relatively new to python and I use primarily py3 for development. > Every once in a while I use a py2 interpreter to see if my tests pass > through. >

Re: zip as iterator and bad/good practices

2015-06-12 Thread Fabien
On 06/12/2015 05:00 PM, Fabien wrote: I've found the izip() function which should do what I want I've just come accross a stackoverflow post where they recommend: from future_builtins import zip which is OK since I don't want to support versions <= 2.6 -- https://mail.python.org/mailman/listi

zip as iterator and bad/good practices

2015-06-12 Thread Fabien
Folks, I am developing a program which I'd like to be python 2 and 3 compatible. I am still relatively new to python and I use primarily py3 for development. Every once in a while I use a py2 interpreter to see if my tests pass through. I just spent several hours tracking down a bug which wa