4 ms·
return [token for token in tokens if token] What the what?
by boynamedsue 6y ago
return [token for token in tokens if token]
What the what?
- mplanchard 6y agoAka list(filter(None, tokens)) Or list(filter(lambda x: bool(x), tokens))
- throwaway1777 6y agoLol. Compact map.
- suresk 6y agoComprehensions in Python are pretty handy, but they can look weird sometimes. This is filtering out `None`, empty string, etc values from a list. It is iterating over a list (tokens) and creating a temporary variable (token). It tests the truthyness of it (if token), which means None and '' will return False and thus be excluded, and then returns it (the first token).