# multiple discontinued ranges

Discussion in 'Python' started by xoff, Nov 10, 2010.

1. ### xoffGuest

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.
Am I obliged to use 2 for loops defining the 2 ranges like this:

for i in range (3,7):
do bla
for i in range (7,17):
do bla

or is there a more clever way to do this?

xoff, Nov 10, 2010

2. ### Paul RudinGuest

xoff <> 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.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

for i in itertools.chain(xrange(3,7), xrange(17,23)):
print i

Paul Rudin, Nov 10, 2010

3. ### Paul RubinGuest

xoff <> 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

Paul Rubin, Nov 10, 2010
4. ### xoffGuest

On 10 nov, 18:13, Paul Rudin <> wrote:
> xoff <> 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.
> > Am I obliged to use 2 for loops defining the 2 ranges like this:

>
> > for i in range (3,7):
> >  do bla
> > for i in range (7,17):
> >  do bla

>
> > or is there a more clever way to do this?

>
> for i in itertools.chain(xrange(3,7), xrange(17,23)):
>     print i

Thank you, that was exactly what I needed!

xoff, Nov 10, 2010
5. ### xoffGuest

On 10 nov, 18:15, Paul Rubin <> 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

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

Thanks again!

xoff, Nov 10, 2010
6. ### MelGuest

xoff wrote:

> 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.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

One horribly clever way is to concoct a 9-th order polynomial to return
3,4,5,6,17,18,19,20,21,22 for input values 0,1,2,3,4,5,6,7,8,9.

The reasonable way is to use two loops as you've done. If the pattern of
discontinuous ranges is really important in your application, you'd perhaps
want to package it up (not tested):

def important_range ():
for x in xrange (3, 7):
yield x
for x in xrange (17,23):
yield x

to be used elsewhere as

for v in important_range():
# use v ...

Mel.

Mel, Nov 10, 2010
7. ### Peter OttenGuest

xoff wrote:

> 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.
> Am I obliged to use 2 for loops defining the 2 ranges like this:
>
> for i in range (3,7):
> do bla
> for i in range (7,17):
> do bla
>
> or is there a more clever way to do this?

>>> for r in xrange(3, 7), xrange(17, 23):

.... for i in r:
.... print i,
....
3 4 5 6 17 18 19 20 21 22

Peter Otten, Nov 10, 2010
8. ### NobodyGuest

On Wed, 10 Nov 2010 09:34:14 -0800, xoff wrote:

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

Because it constructs all three lists (both of the individual ranges and
their concatenation) in memory. For a trivial example, that isn't a
problem; for a real application, it could be.

Nobody, Nov 10, 2010
9. ### Emile van SebilleGuest

On 11/10/2010 9:34 AM xoff said...
> On 10 nov, 18:15, Paul Rubin<> wrote:
>> potentially lots of storage (you should use xrange instead). On the
>> other hand you could just concatenate the lists with +, but I wouldn't

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

I'd assume because concatenation is generally considered expensive.

If you were to do it, you'd likely drop the chain and write:

for i in range(3,7) + range(17,23):

Emile

Emile van Sebille, Nov 10, 2010
10. ### MRABGuest

On 10/11/2010 17:34, xoff wrote:
> On 10 nov, 18:15, Paul Rubin<> 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

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

In Python 3 'range' is a generator, like 'xrange' in Python 2.

MRAB, Nov 10, 2010
11. ### Paul RubinGuest

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

First of all, the outer chain does nothing. Second, concatenating the
two lists creates a new list, consuming storage and taking time copying
all the elements. Third, if there are n lists instead of two, I think
the copying time is O(n**2) in the number of lists because there are n-1
separate copy operations and they keep getting larger.

Paul Rubin, Nov 10, 2010
12. ### Guest

On Nov 10, 10:02 am, Mel <> wrote:
> xoff wrote:
> > 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.
> > Am I obliged to use 2 for loops defining the 2 ranges like this:

>
> > for i in range (3,7):
> > do bla
> > for i in range (7,17):
> > do bla

>
> > or is there a more clever way to do this?

>
> One horribly clever way is to concoct a 9-th order polynomial to return
> 3,4,5,6,17,18,19,20,21,22 for input values 0,1,2,3,4,5,6,7,8,9.
>

And one would want one with integer coefficients... truly horribly
clever! A good example of complex is better than complicated...

> The reasonable way is to use two loops as you've done. If the pattern of
> discontinuous ranges is really important in your application, you'd perhaps
> want to package it up (not tested):
>
> def important_range ():
> for x in xrange (3, 7):
> yield x
> for x in xrange (17,23):
> yield x
>

Reasonable; but DRY: don't repeat yourself. IMNSHO, better would be:

.... def important_range ():
.... for x in [xrange (3, 7), xrange (17,23)]:
.... for y in x:
.... yield y

because face it, you're probably going to add to that list of ranges
anyway, so you should anticipate it (and there's no cost if you don't

To extend that notion, I would imagine that itertools.chain is already
basically defined as:

.... def chain(*args):
.... for x in args:
.... for y in x:
.... yield y

so important_range() can then be simply defined as:

.... def important_range:
.... return chain(xrange (3, 7), xrange (17,23))

Cheers - Chas

> to be used elsewhere as
>
> for v in important_range():
> # use v ...
>

> Mel.

, Nov 11, 2010