Is it "iterate through" or "iterate over" something? [closed]

Just thought about it. Is there a semantic distinction, or are we free to choose?

EDIT I accepted @roygbivs answer, because most of the answers suggested that it is a matter of taste which one to choose, and that it doesn't really change the meaning. In that case, since the through and the over don't carry any information at all (in that particular context), they can be left out.


Solution 1:

"through" and "over" are useless adverbs.

Examples:

  • While iterating this collection, my code throws an error.

  • This code iterates the collection.

  • Your assignment is to write code that iterates the collection.

It's like saying "up north" or "down south". "up" and "down" are useless. Instead you should say, "I am going north for the summer." or "In the south, they grow cotton."

Solution 2:

Not right or wrong, just some stats:

= term =           = Google =   = Github =
"iterate over"      1,440,000   10,569,846 search results
"iterate through"     908,000    5,605,973
"iterate thru"         44,200      141,982
——————————————————————————————————————————
"iterate on"          402,000      187,576
"iterate in"           63,300      117,962
"iterate across"       10,900      208,342
"iterate within"        8,470       14,577
"iterate along"         8,060        6,396
"iterate among"         5,970        2,960
"iterate against"       2,580        3,255
"iterate inside"        2,450       35,060
"iterate amongst"         406       22,274
"iterate amidst"           66            0

Google stats updated on 4/25/2018. Subsequent searches show that results are unstable.
Github stats updated on 4/25/2018. Only code occurrences are counted.

Solution 3:

I think it depends on the object of the preposition; you iterate over the iterator, over the collection, over the container, over the list, over the range, etc. but you iterate through (the contents of) the list, through (the elements of) the container, through the (elements of ) the range, through (the elements of) the collection, etc. In almost all cases they are interchangeable; the only place I would use "over" but not "through" is with respect to the iterator, itself; but for any iterable object, I'd use either "over" or "through".