Take the 2-minute tour ×

As the question says, what would be the difference between:

x.getiterator() and x.iter(), where x is an ElementTree or an Element? Cause it seems to work for both, I have tried it.

If I am wrong somewhere, correct me please.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

getiterator is the ElementTree standard spelling for this method; iter is an equivalent lxml-only method that will stop your code from working in ElementTree if you need it, and appears to have no redeeming qualities whatsoever except saving you from typing 7 more characters for the method name;-).

share|improve this answer
Perfect answer, thank you! –  user225312 Jun 19 '10 at 20:08
@PulpFiction, you're welcome! –  Alex Martelli Jun 19 '10 at 20:27
I'd be interested to see your response to my answer ;-) –  Bruce van der Kooij Aug 13 '10 at 7:57
@Bruce, good point -- I hadn't considered 2.7 (not yet released at that time, I believe). –  Alex Martelli Aug 13 '10 at 15:21
Ah, I overlooked the fact that your answer was from June ;-) –  Bruce van der Kooij Aug 14 '10 at 7:37

The Python documentation for ElementTree states that the getiterator() method has been deprecated starting with version 2.7 and says to use Element.iter(). The lxml API documentation states the same but also mentions that the implementation of getiterator() in lxml diverges from the original ElementTree behavior.

Interestingly enough the documentation also states that "If you want an efficient iterator, use the tree.iter() method instead". Note the word "efficient", which leads me to believe there is most certainly a difference in implementation between getiterator() and iter(), but without checking out the source I can't be 100% sure.

Anyhow, if something has been deprecated it's clear they don't want you to use it.

share|improve this answer

Your Answer


