john john - 5 months ago 8
Python Question

Is there a better way to do this python code?

Looking at this snippet of python code I wrote:

return map(lambda x: x[1],
filter(lambda x: x[0] == 0b0000,
my_func(i)
)
)


(Hoping it's self-explanatory)

I'm wondering if python has a better way to do it?

I learned python several months ago, wrote a couple scripts, and haven't used it much since. It puts me in a weird spot for learning because I know enough to do what I want but don't have the newbie instinct to find the "proper" way. I'm hoping this question will put me back on course...

Answer

I think you want a list comprehension:

[x[1] for x in my_func(i) if x[0] == 0]

List comprehensions are an extremely common Python idiom.