zip as iterator and bad/good practices
sohcahtoa82 at gmail.com
sohcahtoa82 at gmail.com
Fri Jun 12 20:39:28 EDT 2015
On Friday, June 12, 2015 at 5:27:21 PM UTC-7, Chris Angelico wrote:
> On Sat, Jun 13, 2015 at 10:02 AM, <sohcahtoa82 at gmail.com> 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 than just `ints`?
>
> If you use "ints = [...]", it rebinds the name ints to the new list.
> If you use "ints[:] = [...]", it replaces the entire contents of the
> list with the new list. The two are fairly similar if there are no
> other references to that list, but the replacement matches the
> mutation behaviour of remove().
>
> def just_some(ints):
> ints[:] = [i for i in ints if not i % 2]
>
> ChrisA
Ah that makes sense. Thanks.
More information about the Python-list
mailing list