The problem is that update()
in the set returns None
, not the set. This is documented and expected behavior. Assuming you want to use update()
for some reason, you can write your lambda like this:
lambda x, y: x.update(y) or x
The or
clause returns x
if the first clause is false (which None
is).
Indeed, I think you want to use union()
instead of update()
. This is basically the same and returns the result.
lambda x, y: x.union(y)
By the way, you can just write set()
to get an empty set, you do not need set([])
. Thus, the rewritten reduce()
would be:
reduce(lambda x, y: x.union(y), a, set())
Others have posted additional options, each of which matters, making you think about how they work.
source share