5
for iteration in range(len(list) - 1):
  index = iteration +1 #This is the line which has no effect on the inner loop
  for index in range(len(list)):
    if list[iteration] > list[index]:
      newmin  = list[index]
      newminindex = index        
  if iteration != newminindex :
    swapnumbers(list,iteration, newminindex)

The above is a code snippet I wrote for selection sort algorithm. However I see the inner loop start counter always starting from 0. Request for expert comment.

1
  • 1
    When you say range(15) (for example), it really says range(0,15). Using only one argument simplifies it for you (the programmer), but it is really saying the latter. You should simplify it to for index in range(iteration + 1, len(myList)):. Also, try not to reuse variable names like you did here. When you say for index in... you reset the value of index you gave it the line before. As I assume you thought, setting index before does not affect its starting vale in the for loop. The value of index gets changed to the default value of range(oneArg):0, and with 2 args, the first value. Commented Jul 6, 2013 at 0:55

5 Answers 5

9

The for index in range(len(list)) loop executes the loop body with index first set to 0, then 1, then 2, etc. up to len(list) - 1. The previous value of index is ignored and overwritten. If you want index to start at iteration + 1, use the 2-argument form of range:

for index in range(iteration + 1, len(list)):
Sign up to request clarification or add additional context in comments.

Comments

3

You really should be using enumerate for stuff like this, as you can loop through the index and the value at the same time (which will save you the hassle of using two for-loops).

for i, j in enumerate(list):
    print i, j

Your inner loop is overriding the variable index that you defined in the first loop.

Comments

1

Try this instead:

for index in range(iteration + 1, len(l)):  # don't use "list" as a name

index is being reassigned anyway within the for-loop so index = iteration + 1 is not having any effect.

Comments

0
for index in range(iteration + 1, len(list))

2 Comments

Also, what arshajii said about the reserved keyword, "list"
list is not a keyword. It's a builtin identifier; reassigning it is valid, but generally a bad idea.
0

I'd upvote TerryA's answer. However I like to improve his answer to meet OP request and make it conform to Python3

His example with a starting counter 1 would be:

for i, j in enumerate(list,1):
  print (i, j)

and for easier understanding I would replace i and j with something like below. It probably explains it better: (both will work, but it's not obvious what i and j represent)

for list_number, list_value in enumarate(list,1):
  print ("number ", list_number, " is  ", list_value)

Comments

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.