Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to do this python code?

Tags:

python

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...

like image 471
john Avatar asked Nov 20 '10 01:11

john


1 Answers

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.

like image 199
Russell Borogove Avatar answered Sep 21 '22 03:09

Russell Borogove