By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

python loops

P: n/a
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise than 'while'?

C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1

Aug 31 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Putty wrote:
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise than 'while'?

C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1
for i in range(length):
print i
--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

Aug 31 '06 #2

P: n/a
AlbaClause wrote:
for i in range(length):
print i
Or usually better:

for ii in xrange(length):
...

Bye,
bearophile

Aug 31 '06 #3

P: n/a
Putty wrote:
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise than 'while'?

C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1
As AlbaClause had demonstrated you can iterate over indices as well
using the for-statement and the range() function. But you can also
combine iterating over elements and indices at the same time using the
builtin enumerate() type:

for i, item in enumerate(("s1","s2")):
print i,item

0 s1
1 s2

Aug 31 '06 #4

P: n/a

be************@lycos.com wrote:
AlbaClause wrote:
for i in range(length):
print i

Or usually better:

for ii in xrange(length):
~~

I hate ii ;)

Regards,
Kay

Aug 31 '06 #5

P: n/a
Kay Schluehr:
I hate ii ;)
It's not nice looking, I agree. A more explicit name is often better.
But I think ii is better than i because you can find it in the code
(with the Find command of the editor or grep) more easily than i.

Bye,
bearophile

Sep 1 '06 #6

P: n/a

Putty wrote:
In C and C++ and Java, the 'for' statement is a shortcut to make very
concise loops. In python, 'for' iterates over elements in a sequence.
Is there a way to do this in python that's more concise than 'while'?

C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1
Someone else gave an apt reply to a similar question before, but I
don't know when so I'll give the gist:
In languages like C and Java, the for loop index variable is often used
to iterate over members of a sequence or other data type.
In Python many of the data types can be directly iterated over without
having to increment a separate indexing value.

C type language:

length = length_finder(some_datatype);
for(i=0; i<length; i++){
indexed_data = some_datatype[i]
do_stuff(indexed_data)
}

Becomes the more elegant Python:

for indexed_data in some_datatype:
do_stuff(indexed_data)
- Paddy.

Sep 1 '06 #7

P: n/a
be************@lycos.com wrote:
>AlbaClause wrote:
>for i in range(length):
print i

Or usually better:

for ii in xrange(length):
...
xrange used to be better. As I understand it, that's no longer the case.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 3 '06 #8

P: n/a
Tim Roberts wrote:
xrange used to be better. As I understand it, that's no longer
the case.
for short ranges, range is faster in some Python versions, xrange is
faster in some (including 2.4). the difference is usually very small
(the same number of objects are created in both cases).

for long ranges, especially when it's likely that you won't actually
need all the values, xrange is better.

if you *need* a list, range is better.

in python 3.0, range will return an iterator, and xrange will disappear.
if you need a list in 3.0, you will have to do list(range(N)).

</F>

Sep 3 '06 #9

P: n/a
Tim Roberts <ti**@probo.comwrote:
...
xrange used to be better. As I understand it, that's no longer the case.
measuring is better than guessing:

brain:~/codejam alex$ python -V
Python 2.5c1
brain:~/codejam alex$ python -mtimeit 'for x in range(1000):pass'
10000 loops, best of 3: 71.9 usec per loop
brain:~/codejam alex$ python -mtimeit 'for x in xrange(1000):pass'
10000 loops, best of 3: 54 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=range(1000)' 'for x in
y:pass'
10000 loops, best of 3: 44.8 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=xrange(1000)' 'for x in
y:pass'
10000 loops, best of 3: 53.2 usec per loop
brain:~/codejam alex$

So: in 2.5 (2.4 too, do your own timings;-) range is better if you can
create the range once and use it repeatedly, xrange if you have to
create the range each time. But actually:

brain:~/codejam alex$ python -mtimeit -s'from itertools import repeat'
'for x in repeat(None, 1000): pass'
10000 loops, best of 3: 41.3 usec per loop

if you don't actually need the iteration-index in the loop (you just
need to repeat the loop N times), itertools.repeat is even better (well,
if every microsecond counts, anyway;-).
Alex
Sep 9 '06 #10

P: n/a
Jay
Here are my benchmarks for those interested in version 2.4.3

jsherby@montauk:~$ python -V
Python 2.4.3
jsherby@montauk:~$ python -mtimeit 'for x in range(1000):pass'
10000 loops, best of 3: 64.2 usec per loop
jsherby@montauk:~$ python -mtimeit 'for x in xrange(1000):pass'
10000 loops, best of 3: 50.5 usec per loop
jsherby@montauk:~$ python -mtimeit -s'y=range(1000)' 'for x in y:pass'
10000 loops, best of 3: 68.2 usec per loop
jsherby@montauk:~$ python -mtimeit -s'y=xrange(1000)' 'for x in y:pass'
10000 loops, best of 3: 51.4 usec per loop
jsherby@montauk:~$ python -mtimeit -s'from itertools import repeat'
'for x in repeat(None, 1000): pass'
10000 loops, best of 3: 45.6 usec per loop
jsherby@montauk:~$

Alex Martelli wrote:
Tim Roberts <ti**@probo.comwrote:
...
xrange used to be better. As I understand it, that's no longer the case.

measuring is better than guessing:

brain:~/codejam alex$ python -V
Python 2.5c1
brain:~/codejam alex$ python -mtimeit 'for x in range(1000):pass'
10000 loops, best of 3: 71.9 usec per loop
brain:~/codejam alex$ python -mtimeit 'for x in xrange(1000):pass'
10000 loops, best of 3: 54 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=range(1000)' 'for x in
y:pass'
10000 loops, best of 3: 44.8 usec per loop
brain:~/codejam alex$ python -mtimeit -s'y=xrange(1000)' 'for x in
y:pass'
10000 loops, best of 3: 53.2 usec per loop
brain:~/codejam alex$

So: in 2.5 (2.4 too, do your own timings;-) range is better if you can
create the range once and use it repeatedly, xrange if you have to
create the range each time. But actually:

brain:~/codejam alex$ python -mtimeit -s'from itertools import repeat'
'for x in repeat(None, 1000): pass'
10000 loops, best of 3: 41.3 usec per loop

if you don't actually need the iteration-index in the loop (you just
need to repeat the loop N times), itertools.repeat is even better (well,
if every microsecond counts, anyway;-).
Alex
Sep 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.