multiple discontinued ranges

xoff igor.idziejczak at
Wed Nov 10 18:34:14 CET 2010

On 10 nov, 18:15, Paul Rubin <no.em... at nospam.invalid> wrote:
> you could use itertools.chain:
>   from itertools import chain
>   for i in chain(range(3,7), range(17,23)):
>     ...
> I'm assuming you're using python 3.  In python 2 each of those ranges
> expands immediately to a list, so on the one hand they're consuming
> potentially lots of storage (you should use xrange instead).  On the
> other hand you could just concatenate the lists with +, but I wouldn't
> advise that.

I am curious, why wouldn't you advise something like this:
for i in chain(range(3,7) + range(17,23)):

Thanks again!

More information about the Python-list mailing list