using range() in for loops

Ant antroy at gmail.com
Wed Apr 5 05:03:35 EDT 2006


It's not just a Python thing, Java for example generally uses the
idiom:

for (Iterator it = list.iterator(); it.hasNext(); ) {
    Object next = it.next();
    //Do stuff to next
}

Horrible compared to the python idiom of course (though the latest
version supports for (x : list){})

Ruby has something similar in:

list.each do |item|
    print item
end

Again, not as nice as the python idiom IMHO (Though there may be better
ways - I don't know Ruby very well).

So really, all modern programming languages seem to prefer the
loop-over-iterator idiom in preference to looping over a numerical
range.




More information about the Python-list mailing list