Reputation: 6981
In python, What is the most preferred (pythonic) way to do the following:
You are given a list. If the list is not empty, all items in the list are guaranteed to be strings. Each item in the list is either the empty string, or is guaranteed to return
True
ifisdigit()
is called on the item.
Starting with such a list, what is the most elegant way to end up with a list such that it has all items from the original list, except for the empty strings?
Upvotes: 0
Views: 78
Reputation: 1121634
Using filter()
with the default identity function (None
):
newlist = filter(None, origlist)
alternatively, a list comprehension:
newlist = [el for el in origlist if el]
Upvotes: 6