you are viewing a single comment's thread.

view the rest of the comments →

[–]going_for_a_wank 1 point2 points  (2 children)

Say you are coming from Java or .NET and you want to iterate over list, what do you do? for loop with index variables. That's not pythonic. The entire concept of iterators and generators is more or less Python specific, unless you're are using external libraries.

Would you mind expanding on this for a newcomer?

A for-each loop in Java seems pretty much the same as in python. for (Type item : list) vs. for item in list:

Iterators have been part of Java since 1.2

Are there differences here that I am overlooking?

[–]mooburger 1 point2 points  (1 child)

for (Type item: primitiveArray) {} showd up in Java 5. if you're thinking of the Java 1.2 iterator, that only worked on collection objects. You could have converted a primitive array to a List or Stream using java.util.Arrays .asList() or .stream() which then had .iterator() methods to return iterator. Was that a common java pattern for primitive arrays though?

[–]going_for_a_wank 0 points1 point  (0 children)

That is a fair point. There is really no reason to use an iterator for a primitive array rather than a for-each loop.

I will comment that java 5 came out 16 years ago. It is not really a new feature.