2

I have been struggling with this quite a bit, and was wondering if there is a solution for this.

I would like to use the range(start,stop,step) function in Python, but I would like to use a different order than what the normal functionalities allow. From what I understand the range function can do 3 things:

  1. 0, 1, 2, 3 etc
  2. 10, 9, 8, 7 etc
  3. 2, 4, 6, 8 etc

Now I am looking for the following order: 0, 10, 1, 9, 2, 8, 3, 7 etc

In this case 10 in the len(df), so the last row of the df.

3
  • The range function can't do this, it uses a fixed step size. Commented Dec 7, 2020 at 11:42
  • Range is not done for custom ordered list of numbers. You can either use a list or create your own generator if you prefer. Commented Dec 7, 2020 at 11:43
  • Does this answer your question? For loop with custom steps in python Commented Dec 7, 2020 at 11:44

3 Answers 3

3

You can create a generator to do that:

def my_range(n):
    """Yields numbers from 0 to n, in order 0, n, 1, n-1..."""
    low = 0
    high = n
    while low <= high:
        yield low
        if high != low:
            yield high
        low += 1
        high -= 1
    

Some examples:

print(list(my_range(10)))
# [0, 10, 1, 9, 2, 8, 3, 7, 4, 6, 5]

for i in my_range(5):
    print(i)
    
0
5
1
4
2
3

This will provide every number in the interval exactly once, and lazily, just as range.


To answer the question in your comment:

if you want to mix the numbers in your_list = [1,3,4,5,7,9,10,12,13,14], you can just use this function to generate the indices:

your_list = [1,3,4,5,7,9,10,12,13,14]
for index in my_range(len(your_list)-1):
    print(your_list[index], end=' ')

# 1 14 3 13 4 12 5 10 7 9 

or you could build a new list in the mixed order:

new = [your_list[index] for index in my_range(len(your_list)-1)]
print(new)
# [1, 14, 3, 13, 4, 12, 5, 10, 7, 9]
Sign up to request clarification or add additional context in comments.

2 Comments

That is a great solution, thanks!! One additional question, would it also be possible to do this when the numbers in the list are not gradually increasing? E.g. the numbers in the list are 1,3,4,5,7,9,10,12,13,14. And then the result should be the lowest followed by the highest and repeat: [1,14,3,13,4,12,5,10,7,9].
I added the answer to your comment.
2

range just has start, stop, step params. But you can achieve what you want by zipping two ranges together along with the chain.from_iterable function:

from itertools import chain

for val in chain.from_iterable(zip(range(11), range(10, -1, -1))):
    print(val)

# 0 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 0

Note this solution repeats values, if you want no repeated values, then a generator is the way to go.

4 Comments

ValueError: range() arg 3 must not be zero
@ThierryLathuille Oops typo, fixed.
This produces 0 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 , with some duplicated values, which is probably not what the OP expected...
Now it's 0 5 1 4 2 3 3 2 4 1
-1

To keep things simple and clear the range function cannot do this. Since it only allows to either increment or decrement at once.

but this can be achieved with loops and variables.

this is how to do it

for i in range(0,11):
    print(i,10-i,end=" ")

this code will do it.

5 Comments

but this can be achieved with loops and variables : how?
from itertools import chain for val in chain(*zip(range(10), range(10, -1, 0))): print(val)
That's the same as @Moosefeather 's answer.
i have edited the answer plz check if it helps
Well.. I guess it's better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.