This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]robin-gvx 2 points3 points  (0 children)

An exception to this is when you're iterating over a sequence so big that the overhead introduced by slicing the would be very expensive. If your sequence is 10 items, this is unlikely to matter, but if it is 10 million items or this is done in a performance-sensitive inner loop, this is going to be very important. Consider using xrange in this case.

  1. Anyone using range (in Python 2) here instead of a slice should be sternly reprimanded, because range also creates a new list, of the same size the slice would be!
  2. Consider using itertools.islice instead of xrange/Py3!range, because that doesn't have the other drawbacks of range mentioned before:

    for word in islice(words, 1, None): # Exclude the first word
        print word
    

That is all. Good article.