python loops

P

Putty

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
 
A

AlbaClause

Putty said:
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


--
 
K

Kay Schluehr

Putty said:
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
 
B

bearophileHUGS

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
 
P

Paddy

Putty said:
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
do_stuff(indexed_data)
}

Becomes the more elegant Python:

for indexed_data in some_datatype:
do_stuff(indexed_data)


- Paddy.
 
F

Fredrik Lundh

Tim said:
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>
 
A

Alex Martelli

Tim Roberts said:
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
 
J

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:~$
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top