[Solved] Understand python syntax reduce lambda


set(y) | x is the union of set(y) and x.

The set() at the end is the initial value for reduce(), the first value of x that will be used so that the function can get going. The first value in structures will be the first value of y.

If you left out the initial value, i.e. something like:

charset = list(reduce(lambda x, y: set(y) | set(x), structures))

then if structures is empty this will raise:

TypeError: reduce() of empty sequence with no initial value

and if structures has length one then reduce will just return the one element, not converted to a set.

In general | is the bitwise OR operator but sets override several operators for their own meaning. For future reference the character is called a pipe, so for example searching for “python set pipe” yields some answers.

solved Understand python syntax reduce lambda