Remove matching subelement. Unlike the find methods, this method compares elements based on identity, NOT ON tag value or contents. To remove subelements by other means, the easiest way is to use a list comprehension to select what elements to keep, and then use sli
(self, subelement)
| 252 | raise TypeError('expected an Element, not %s' % type(e).__name__) |
| 253 | |
| 254 | def remove(self, subelement): |
| 255 | """Remove matching subelement. |
| 256 | |
| 257 | Unlike the find methods, this method compares elements based on |
| 258 | identity, NOT ON tag value or contents. To remove subelements by |
| 259 | other means, the easiest way is to use a list comprehension to |
| 260 | select what elements to keep, and then use slice assignment to update |
| 261 | the parent element. |
| 262 | |
| 263 | ValueError is raised if a matching element could not be found. |
| 264 | |
| 265 | """ |
| 266 | try: |
| 267 | self._children.remove(subelement) |
| 268 | except ValueError: |
| 269 | # to align the error type with the C implementation |
| 270 | if isinstance(subelement, type) or not iselement(subelement): |
| 271 | raise TypeError('expected an Element, not %s' % |
| 272 | type(subelement).__name__) from None |
| 273 | # to align the error message with the C implementation |
| 274 | raise ValueError(f"{subelement!r} not in {self!r}") from None |
| 275 | |
| 276 | def find(self, path, namespaces=None): |
| 277 | """Find first matching element by tag name or path. |