The problem is that update()
on a set returns None
, not the set. This is documented and expected behavior. Assuming you want to use update()
for some reason, you could write your lambda as follows:
lambda x, y: x.update(y) or x
The or
clause returns x
if the first clause is "falsy" (which None
is).
Really, though, I think you want to use union()
instead of update()
. It does mostly the same thing and returns the result.
lambda x, y: x.union(y)
BTW, you can just write set()
to get an empty set, you don't need set([])
. So the rewritten reduce()
would be:
reduce(lambda x, y: x.union(y), a, set())
Others have posted additional options, each of which has value for making you think about how they work.