Two range in for loop python

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

Can you have 2 variables in a for loop Python?

The use of multiple variables in a for loop in Python can be applied to lists or dictionaries, but it does not work for a general error. These multiple assignments of variables simultaneously, in the same line of code, are known as iterable unpacking.

Can you add two ranges Python?

Python doesn't have a built-in function to merge the result of two range[] output. However, we can still be able to do it. There is a module named 'itertools' which has a chain[] function to combine two range objects.

How do you write a range in a for loop in Python?

In Python, Using a for loop with range[] , we can repeat an action a specific number of times..
Here, start = 0 and step = 1 as a default value..
If you set the stop as a 0 or some negative value, then the range will return an empty sequence..
If you want to start the range at 1 use range[1, 10] ..

Can you have two arguments in a for loop?

With two arguments in the range function, the sequence starts at the first value and ends one before the second argument. Programmers use x or i as a stepper variable.

Chủ Đề