Skip to Content
Trees: Python
Tree Implementation IIIa: Tuning the Pruning

Trees are an abstract idea that we’re making concrete in Python. When implementing these abstract data structures, it’s important to leverage the features of your language.

Let’s refactor .remove_child() to use Python’s list comprehension. As a quick refresher on list comprehension:

nums = [1, 2, 3, 4, 5] evens = [num for num in nums if num % 2 == 0] # [2, 4]



Replace the existing code in .remove_child and assign self.children to a list comprehension which performs the same logic.

Folder Icon

Sign up to start coding

Already have an account?