3 ms·
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', 'i
by swang 14y ago
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
- Hemospectrum 14y agoThe list comprehension rules seem to be lifted straight from Python, where the "foo for bar" part always comes first.