459,453 Members | 937 Online
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

for loop

 P: n/a Hello, I have seen a lot of way to use the for statement, but I still don't know how to do: for i=morethanzero to n or for i= 5 to len(var) of course I kno wthat I can use a while loop to achieve that but if this is possible whith a for one, my preference goes to the for. regards, Dominique. Jul 18 '05 #1
7 Replies

 P: n/a houbahop wrote: for i=morethanzero to n for i in range(morethanzero, n): ... for i= 5 to len(var) for i in range(5, len(var)): ... although range(len(var)) is usually not what you want, because you can just do: for item in itertools.islice(var, 5, None): ... or if you really do need the index too: for i, item in itertools.islice(enumerate(var), 5, None): ... Steve Jul 18 '05 #2

 P: n/a > for i in range(morethanzero, n): ... for i= 5 to len(var) for i in range(5, len(var)): ... Better use xrange - it doesn't create an actual list, but instead an iterator enumerating the elements. That way more memory and cpu efficient. -- Regards, Diez B. Roggisch Jul 18 '05 #3

 P: n/a Diez B. Roggisch wrote:for i in range(5, len(var)): ... Better use xrange - it doesn't create an actual list, but instead an iterator enumerating the elements. That way more memory and cpu efficient. I could've sworn I read somewhere in the past that xrange was supposed to be slower than range in some situation, but at least for some simple cases in Python 2.4, it seems to be about as fast or faster: python -m timeit "for i in range(100): a = i*2" 10000 loops, best of 3: 21.8 usec per loop python -m timeit "for i in xrange(100): a = i*2" 10000 loops, best of 3: 19.4 usec per loop python -m timeit "for i in range(10000): a = i*2" 100 loops, best of 3: 2.18 msec per loop python -m timeit "for i in xrange(10000): a = i*2" 100 loops, best of 3: 2.29 msec per loop python -m timeit "for i in range(100000): a = i*2" 10 loops, best of 3: 25.5 msec per loop python -m timeit "for i in xrange(100000): a = i*2" 10 loops, best of 3: 20.7 msec per loop I hardly ever use (x)range for anything (because you can almost always use a for loop to loop over items instead), but I'll file this info away mentally for the next time I do. Thanks! Steve Jul 18 '05 #4

 P: n/a houbahop wrote: Hello, I have seen a lot of way to use the for statement, but I still don't know how to do: for i=morethanzero to n or for i= 5 to len(var) of course I kno wthat I can use a while loop to achieve that but if this is possible whith a for one, my preference goes to the for. regards, Dominique. I think the key point that hasn't been made here is what a for statement is really doing in python... From the online documentation: "The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object" Neither of the statements: for i=morethanzero to n for i= 5 to len(var) create a sequence or iterable object, and that is why they don't work. That is why previous posts in this thread have suggested using range(), xrange(), etc. Because they create a sequence or iterable object. When first using python, I recall that this distinction was not clear to me, as I was used to a more traditional for statement (as in C/C++): for ( initialise ; test ; update ) Essentially, python's for statement is more like a foreach statement in many other languages (Perl, C#, etc). These statements essentially reduce the traditional form above to what many consider a more readable form: foreach item in collection: In order to transform the tradition for statement into this more readable form, each language requires that their collections being iterated over satisfy a precondition defined by the language (in python this precondition is that the collection is iterable). While this restriction may seem a little strange to people who are used to the more traditional for statement form, the result of this approach is often a more readable and clear statement. Regards, Michael Loritsch Jul 18 '05 #5

 P: n/a thank you, Whith python, It seems that all that I have learn in other languages is not directly usable :) but looks like python is more efficient. dominique. a écrit dans le message de news: 11**********************@c13g2000cwb.googlegroups. com... houbahop wrote: Hello, I have seen a lot of way to use the for statement, but I still don't know how to do: for i=morethanzero to n or for i= 5 to len(var) of course I kno wthat I can use a while loop to achieve that but if this is possible whith a for one, my preference goes to the for. regards, Dominique. I think the key point that hasn't been made here is what a for statement is really doing in python...From the online documentation: "The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object" Neither of the statements: for i=morethanzero to n for i= 5 to len(var) create a sequence or iterable object, and that is why they don't work. That is why previous posts in this thread have suggested using range(), xrange(), etc. Because they create a sequence or iterable object. When first using python, I recall that this distinction was not clear to me, as I was used to a more traditional for statement (as in C/C++): for ( initialise ; test ; update ) Essentially, python's for statement is more like a foreach statement in many other languages (Perl, C#, etc). These statements essentially reduce the traditional form above to what many consider a more readable form: foreach item in collection: In order to transform the tradition for statement into this more readable form, each language requires that their collections being iterated over satisfy a precondition defined by the language (in python this precondition is that the collection is iterable). While this restriction may seem a little strange to people who are used to the more traditional for statement form, the result of this approach is often a more readable and clear statement. Regards, Michael Loritsch Jul 18 '05 #6

 P: n/a > I hardly ever use (x)range for anything (because you can almost always use a for loop to loop over items instead), but I'll file this info away mentally for the next time I do. Thanks! Thats true for me too - and since enumerate was introduced, the number of applications of xrange have decreased further. -- Regards, Diez B. Roggisch Jul 18 '05 #7

 P: n/a If you are used to the C kind of for loops, avoid mistakes like this one: for i in range(1,6): .... print i .... i+=2 .... 1 2 3 4 5 Obvious if you know you are iterating over a sequence. Jul 18 '05 #8