Hacker News new | ask | show | jobs
by boynamedsue 1955 days ago

  return [token for token in tokens if token]
What the what?
3 comments

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

Aka

    list(filter(None, tokens))
Or

    list(filter(lambda x: bool(x), tokens))
Lol. Compact map.