Today is election day in the states. Lots of free coffee and donut offers floating around for those few people wearing their I voted sticker.

Tomorrow I head out for my trip to Iceland. From Florida to Iceland in the Fall, makes perfect sense right? It was an excuse for me to purchase some new warm wear and jogging shoes, have to keep up with the exercise routine even on vacation. I should be able to maintain the blog from there keeping the spirit of blog every day in November.

Snippet of Python
In an effort to always have something a little Python related in all my posts, here is how to use list comprehension to filter out no value items from a list.

foo = ["A", "", "C", None, "E"]
bar = [i for i in foo if i]
print bar
['A', 'C', 'E']

3 Comments

  1. Zachary Voase says:

    You know, all that list comprehension can just be done like this:

    bar = filter(None, foo)

    Filtering with None as the first argument just filters everything False out, which includes False, [], {}, 0, ” and None.

  2. elblogg » Blog Archive » Should have guessed it says:

    [...] I had one of these realizations, while reading pieceofpy’s snippet of the [...]

  3. Wayne says:

    Never looked at the doc string for filter before. Didn’t know you could pass in None, always thought it needed a comparison method. Thanks Zach.

    Also noted that filter will maintain type information for you. Pass in a string, get back a string. Pass in a tuple, get back a tuple. Very nice.

Leave a Reply