from collections import deque

def walk(node):
    queue = deque([node])
while queue:
    node = queue.popleft()
if isinstance(node, tuple):
    queue.extend(node[1:])
yield node
How to do a "tree walk" recursively on an Abstract Syntax Tree?