I have a nodelist object that yields a bunch of node objects on iteration. I need to increment these nodes based on some random condition (like node.x > 17). Here’s what I’m doing right now:
for node in nodelist:
if node.x > 17:
node.x += 1
I can’t do map(lambda node: node.x += 1, nodelist) because lambda can’t contain assignment. I cannot do nodelist = [node.x + 1 for node in nodelist if...] because a nodelist object consists of more than just its child nodes.
Is there a way to make this shorter/cleaner?
I think it’s already pretty short and clean. Python provides some powerful tools, but sometimes simple is best; this seems like one of those times.
I also think what you have is more readable than any equivalent one-liner (that I can come up with).
Ok, that said, you could do this:
That could even be compressed into one line. But I honestly prefer the way you have it now.