'xml tree remove elements

I am trying to remove elements from my tree based on the elements attributes.

import xml.etree.cElementTree as ET
import requests

xml = requests.get("https://iptv-org.github.io/epg/guides/ca/sportsnet.ca.epg.xml").text
tree = ET.ElementTree(ET.fromstring(xml))
root = tree.getroot()

for elem in tree.iter():
    if elem.tag == "channel" or elem.tag == "programme":
        if elem.attrib.get("id", "") == "WWENetworkCanada.us" or elem.attrib.get("channel", "") == "WWENetworkCanada.us":
            pass
        else:
            print("removing")
            root.remove(elem)

ET.dump(root)

The dump still has the elements I am trying to remove, even though I see "removing". Can anyone see why?

I am expecting everything that is not "WWENetworkCanada.us" to be removed.

Thanks, Chris



Solution 1:[1]

It was failing because I was changing indexes as I iterating and removing. What I needed to do was for elem in list(tree.iter()):. This is something I learnt some time ago (on here) that lets you remove while iterating without creating a "temp" list.

Solution 2:[2]

Saw you answered your own question, but thought I'd share as an alternate approach. Simply use findall() to find and remove any values that don't match what you are looking for

Remove Unwanted Elements using findall()

for elem in tree.findall("./channel/[@id!='WWENetworkCanada.us']"):
    root.remove(elem)
    
for elem in tree.findall("./programme/[@channel!='WWENetworkCanada.us']"):
    root.remove(elem)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris
Solution 2 Stephan