Lets say i'm trying to print 0 to 25 and 75 to 100. Right now I have:
for x in range(0, 26):
print(x)
for x in range(75, 101):
print(x)
Is there any way to combine these into a single for loop that lead to:
print(x)
?
Something along the lines of:
for x in range(0, 26) and range(75, 101):
print(x)
You need the itertools.chain()
:
Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.
from itertools import chain
for x in chain(range(0, 26), range(75, 101)):
print(x)
Works for both Python 2 and 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With