5

What is the best way of doing this in Python?

for (v = n / 2 - 1; v >= 0; v--)

I actually tried Google first, but as far as I can see the only solution would be to use while.

asked Apr 12, 2010 at 21:21

4 Answers 4

15

I would do this:

for i in reversed(range(n // 2)):
 # Your code
 pass

It's a bit clearer that this is a reverse sequence, what the lower limit is, and what the upper limit is.

answered Apr 12, 2010 at 22:18
Sign up to request clarification or add additional context in comments.

4 Comments

@ ΤΖΩΤΖΙΟΥ: true, but this does not have the benefit you may think. range() in pre-3.0 pythons pulls all the data at once. xrange() acts like a generator, pulling only one item at a time. But if you are going to reverse a sequence, you need all the data at once, so reversed(range()) and reversed(xrange()) will work in the same way: they'll each have all the data pulled before it is reversed.
I actually ended up using this, big thanks! A follow-up question: I know I've seen it, but how do I print a range() (an iterator I guess)? Edit: thanks hughdbrown for the performance note, I'll consider that in more complex applications
@hughdbrown: it does have the benefit you think I might be thinking. Check the dir(xrange) output and note the __reversed__ special method.
@ ΤΖΩΤΖΙΟΥ: The things I learn. docs.python.org/library/functions.html#reversed explains that reversed can consume __reversed__ method if present -- so that xrange() could be implemented optimally, as ΤΖΩΤΖΙΟΥ suggests. See also: python.org/dev/peps/pep-0322
13

The way to do it is with xrange():

for v in xrange(n // 2 - 1, -1, -1):

(Or, in Python 3.x, with range() instead of xrange().) // is flooring division, which makes sure the result is a whole number.

answered Apr 12, 2010 at 21:24

1 Comment

hughdbrown's version should be marked as the correct answer
5
for v in range(n//2, -1, -1)

However, in 90% of the cases when you would have used a for loop in C/Java/C#/VB, what you really want is list comprehension:

listOfStuff = [doSomethingWith(v) for v in range(n//2, -1, -1)]
answered Apr 12, 2010 at 21:25

1 Comment

-1: This has one too many items. It incorrectly includes n//2.
0
for v in xrange(n/2 - 1, 0, -1):
 #your code here

Where v and n are ints or treated as ints. This means that the division will be an integer division, i.e., 1/2 == 0 is True.

Note: This is for Python 2.x .

answered Apr 12, 2010 at 21:25

1 Comment

-1: This has one too few items. It incorrectly omits 0. Separately, in python3 it fails because n/2 generates a float (TypeError: 'float' object cannot be interpreted as an integer). But maybe that's why you called out that it is for python 2.x.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.