0

Watch index n ( and y)

n = 0
while(0 < y):
  result[ n += 1] = items[y -= 1]

So the above is accepted syntax. Is there an elegant way to do this, in the order of Java's

result[ n++] = items[--y]
1
  • @Lev Levitsky, if this helps: for an example of what I am trying to do see en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/… . I am wondering about combining the two steps: array[i] = a and i += 1. I suppose it may not be such a big deal. Nonetheless that's the question. Commented Dec 6, 2012 at 19:06

1 Answer 1

1

No, this is not accepted syntax.

If I understand correctly what you are trying to do, the pythonic way would be

result = items[::-1]

or

result = list(reversed(items))

(the reversed function itself returns an iterator, not a list).

Sign up to request clarification or add additional context in comments.

2 Comments

That's not what I am trying to do. The question is straight forward: auto incrementing the index n: result[ n += 1]. Thanks for your response, though.
@kasavbere The point is, in Python you usually don't use indexes in operations like this. In this case you are even avoiding the need of an explicit loop. Using unjustified C-like or Java-like constructs in Python is commonly considered bad practice.

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.