Sometimes I encounter situations where I would like to capture the match point within a comprehension, for example, in this segment:
for child1 in node1.getChildren():
if child1.getData() in [child2.getData() for child2 in node2.getChildren()]:
# somehow I want the list comprehension to side effect and capture child2 match point
doNodes(child1, child2)
# Or if we capture an index:
doNodes(child1, node2.getChild(idx)
else:
doOther()
Is there a way to do this (either capture child2 or its index) without opening another loop for node2 - even using something other than compresensions.
In other words: we are just looking to shortcut the inner loop to avoid longer code and using flags to test the loop match.
Note: probably similar to this one: Finding the index of elements based on a condition using python list comprehension