Lucky numbers in Python

Ian Kelly ian.g.kelly at gmail.com
Wed Apr 29 21:57:44 CEST 2015


On Wed, Apr 29, 2015 at 12:24 PM, Cecil Westerhof <Cecil at decebal.nl> wrote:
> I was wondering if there is a way to do this:
>             for del_index in range((sieve_len // skip_count) * skip_count - 1,
>                                   skip_count - 2, -skip_count):
>                 del sieve[del_index]
> in a more efficient way.

You can delete using slices.

del sieve[(sieve_len // skip_count) * skip_count - 1 : skip_count - 2
: -skip_count]

Now you no longer need to do the iteration in reverse, which makes the
slicing simpler:

del sieve[skip_count - 1 : (sieve_len // skip_count) * skip_count : skip_count]

And although it's not clear to me what this is supposed to be doing,
you probably no longer need the middle term if the intention is to
continue deleting all the way to the end of the list (if it is then I
think you have a bug in the existing implementation, since the last
item in the list can never be deleted).

del sieve[skip_count - 1 :: skip_count]



More information about the Python-list mailing list