this way if you really need to take over the indexing logic explicitlyyou can do it with while loopi while len( )print( [ ]end=' + while loop iteration you can also do manual indexing with forthoughif you use range to generate list of indexes to iterate through it' multistep processbut it' sufficient to generate offsetsrather than the items at those offsetsx 'spamlen(xlength of string list(range(len( ))all legal offsets into [ for in range(len( ))print( [ ]end='manual range/len iteration note that because this example is stepping over list of offsets into xnot the actual items of xwe need to index back into within the loop to fetch each item if this seems like overkillthoughit' because it isthere' really no reason to work this hard in this example although the range/len combination suffices in this roleit' probably not the best option it may run slowerand it' also more work than we need to do unless you have special indexing requirementyou're better off using the simple for loop form in pythonfor item in xprint(itemend='use simple iteration if you can as general ruleuse for instead of while whenever possibleand don' use range calls in for loops except as last resort this simpler solution is almost always better like every good rulethoughthere are plenty of exceptions--as the next section demonstrates sequence shufflersrange and len though not ideal for simple sequence scansthe coding pattern used in the prior example does allow us to do more specialized sorts of traversals when required for examplesome algorithms can make use of sequence reordering--to generate alternatives in searchesto test the effect of different value orderingsand so on such cases may require offsets in order to pull sequences apart and put them back togetheras in the while and for loops