Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Haven't had the chance to download and look through it but an example caught my eye.

Was anyone else confused that this,

# filter list => [ 'ipod', 'ipad', 'iphone' ]

('i' + p) for p in ['mac', 'pod', 'pad', 'phone'] if p.startsWith('p')

applies a filter against the list, rather than limit what gets prefixed with an 'i'?

I'm assuming then there's some code to keep the mapping and the entire list as well. Something like this?

('i' + p) if p.startsWith('p') for p in ['mac', 'pod', 'pad', 'phone']

It just seemed surprising to me that an if statement can cause a change in the list length.

Edit: Ah this seems to be how filtering works in python



The list comprehension rules seem to be lifted straight from Python, where the "foo for bar" part always comes first.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: