multiple discontinued ranges

Paul Rubin no.email at nospam.invalid
Wed Nov 10 12:15:38 EST 2010


xoff <igor.idziejczak at gmail.com> writes:
> I was wondering what the best method was in Python programming for 2
> discontinued ranges. e.g. I want to use the range 3 to 7 and 17 to 23.

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.



More information about the Python-list mailing list