I have a couple of simple loops like so:
for i in range [30, 52]:
#do some stuff here
for i in range [1, 18]:
#do some more stuff
What I would like to is condense this into one loop using syntax of the order:
for i in range[[30, 52], [1, 18]:
#do some stuff
I realise that syntax will not work, but that is the basic concept of what I need. I've seen people using zip to iterate two ranges simultaneously, but this is not what I need.
Any ideas?
asked Jun 21, 2015 at 12:21
5
From //docs.python.org/2/library/itertools.html#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. Used for treating consecutive sequences as a single sequence.
Example:
import itertools as it
for i in it.chain[range[30, 52], range[1, 18]]:
print[i]
for python 3
you can loop into the two ranges together
Example:
import itertools as it
for i, x in it.zip_longest[range[30, 52], range[1, 18]]:
print[i, x]
A.Raouf
2,0211 gold badge24 silver badges33 bronze badges
answered Jun 21, 2015 at 12:24
utdemirutdemir
25.8k10 gold badges61 silver badges81 bronze badges
1
You can convert the two iterators for your ranges to lists and then combine them with an addition:
for i in list[range[30, 52]] + list[range[1, 18]]:
# something
kalehmann
4,5536 gold badges24 silver badges35 bronze badges
answered Aug 23, 2019 at 11:07
Oleg KOleg K
711 silver badge1 bronze badge
for i in range[30, 52] + range[1, 18]:
#something
answered Apr 9, 2018 at 10:56
2
1. For loop with range
In the previous lessons we dealt with sequential programs and conditions. Often the program needs to repeat some block several times. That's where the loops come in handy. There are for
and while
loop operators in Python, in this lesson we cover for
.
for
loop iterates over any sequence. For
instance, any string in Python is a sequence of its characters, so we can iterate over them using for
:
for character in 'hello': print[character]
Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order. Such a sequence of integer can be created using the function range[min_value, max_value]
:
for i in range[5, 8]: print[i, i ** 2] print['end of loop'] # 5 25 # 6 36 # 7 49 # end of loop
Function range[min_value, max_value]
generates a
sequence with numbers min_value
, min_value + 1
, ..., max_value - 1
. The last number is not included.
There's a reduced form of range[] - range[max_value]
, in which case min_value is implicitly set to zero:
for i in range[3]: print[i] # 0 # 1 # 2
This way we can repeat some action several times:
for i in range[2 ** 2]: print['Hello, world!']
Same as with if-else, indentation is what specifies which instructions are
controlled by for
and which aren't.
Range[] can define an empty sequence, like range[-5]
or range[7, 3]
. In this case the for-block won't be executed:
for i in range[-5]: print['Hello, world!']
Let's have more complex example and sum the integers from 1 to n inclusively.
result = 0 n = 5 for i in range[1, n + 1]: result += i # this ^^ is the shorthand for # result = result + i print[result]
Pay attention that maximum value in range[] is n + 1
to make i
equal to
n on the last step.
To iterate over a decreasing sequence, we can use an extended form of range[] with three arguments - range[start_value, end_value, step]
. When omitted, the step is implicitly equal to 1. However, can be any non-zero value. The loop always includes start_value and excludes end_value during iteration:
for i in range[10, 0, -2]: print[i] # 10 # 8 # 6 # 4 # 2
Advertising by Google, may be based on your interests
2. setting the function print[]
By default, the function print[]
prints all its arguments separating them by a space and the puts a newline symbol after it. This behavior can be changed using keyword arguments sep
[separator] and end
.
print[1, 2, 3] print[4, 5, 6] print[1, 2, 3, sep=', ', end='. '] print[4, 5, 6, sep=', ', end='. '] print[] print[1, 2, 3, sep='', end=' -- '] print[4, 5, 6, sep=' * ', end='.']
Advertising by Google, may be based on your interests