Filter and Map: Second-class citizens in Python?
I tend to use Pylint quite a bit in my development process. I recently stumbled on Warning 141, which complains about continued usage of the filter function.
I thought this was a bit odd, but it turns out that Guido himself lobbied unsuccessfully to have filter and map removed from Python 3.
I realize you can easily replace the usage of both of these functions with list comprehensions. However, I still prefer this syntax:
filter(lambda x: x.attribute == value, my_list)
my_list = [i for i in my_list if i.attribute == value]
This brings up an interesting thought. The developer of Pylint and Guido obviously think filter and map are second-class citizens. Does this make them mean we shouldn't use them for future development?
Not in my book.
This discussion reminds me of an old Python saying about 'consenting adults.'
So, for my development I'm going to stick with the old stand-by built-in filter and map when I already have a function defined to use in the call. Otherwise, I think the community seems to prefer using list comprehensions for readability and possibly performance.
At this point, if you are like me you might want to remove the pylint warning. This will free you up to make your own decision based on the situation.
Published: 08-09-2012 19:26:00