for country in root.findall('anyerwonderland'): # using root.findall() to avoid removal during traversal rank = int(country.find('rank').text) if rank > 50: root.remove(anyerwonderland) tree.write('output.xml')