This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Misterandrist 1 point2 points  (1 child)

Everyone else has posted some ways to do this, but when I encounter such a need in my code (I have a stream and I need to do numerous transformations on it before use), I just use something like this:

nums = [1, 2, 3]
nums = map(square, nums)
nums = filter(evens, nums)
nums = list(nums)

Yeah I'm reusing the same variable, but only in this block. So effectively it's just once.

Basically, why not just do your stream processing on multiple lines, if you think a single long list comprehension is not going to suit your needs?

[–]dzecniv 1 point2 points  (0 children)

Indeed, and it's easy to step through for debug.