You cannot really maintain state in a filter
/lambda
expression (unless abusing the global namespace). You can however achieve something similar using the accumulated result being passed around in a reduce()
expression:
>>> f = lambda a, b: (a.append(b) or a) if (b not in a) else a
>>> input = ["foo", u"", "bar", "", "", "x"]
>>> reduce(f, input, [])
['foo', u'', 'bar', 'x']
>>>
You can, of course, tweak the condition a bit. In this case it filters out duplicates, but you can also use a.count("")
, for example, to only restrict empty strings.
Needless to say, you can do this but you really shouldn't. :)
Lastly, you can do anything in pure Python lambda
: http://vanderwijk.info/blog/pure-lambda-calculus-python/