We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9ece9cd commit 40db798Copy full SHA for 40db798
Doc/library/xml.etree.elementtree.rst
@@ -251,12 +251,18 @@ We can remove elements using :meth:`Element.remove`. Let's say we want to
251
remove all countries with a rank higher than 50::
252
253
>>> for country in root.findall('country'):
254
+ ... # using root.findall() to avoid removal during traversal
255
... rank = int(country.find('rank').text)
256
... if rank > 50:
257
... root.remove(country)
258
...
259
>>> tree.write('output.xml')
260
261
+Note that concurrent modification while iterating can lead to problems,
262
+just like when iterating and modifying Python lists or dicts.
263
+Therefore, the example first collects all matching elements with
264
+``root.findall()``, and only then iterates over the list of matches.
265
+
266
Our XML now looks like this:
267
268
.. code-block:: xml
0 commit comments