multiple discontinued ranges

MRAB python at mrabarnett.plus.com
Wed Nov 10 15:51:21 EST 2010


On 10/11/2010 17:34, xoff wrote:
> 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)):
>
In Python 3 'range' is a generator, like 'xrange' in Python 2.



More information about the Python-list mailing list