zip as iterator and bad/good practices

Fabien fabien.maussion at gmail.com
Fri Jun 12 11:34:03 EDT 2015


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 see the problem here. The branch object in the zip list
> and the branch object in branches should be the*same*  object, so the
> downstream branch update should be reflected when you visit it later
> in the iteration, regardless of whether zip returns a list or an iterator.
>
> Tangentially, unless you're using id_branch for something else that
> isn't shown here, is it really necessary to search the list for the
> downstream branch when it looks like you already have a reference to
> it? Could the above simply be replaced with:
>
>      branch.flows_to.property.append(stuff_i_computed)

Thanks a lot for your careful reading! I overly simplified my example 
and indeed this line works fine. I was adding things to "stuffs" too, 
which is a list of lists... Sorry for the confusion!



More information about the Python-list mailing list