user755315
user755315

Reputation: 86

Multiple set unions along with list comprehension

I am trying to understand this code:

edit_two_set = set()
edit_two_set = set.union(*[edit_two_set.union(edit_one_letter(w, allow_switches)) for w in one])

Here one is a set of strings. allow_switches is True. edit_one_letter takes in one word and makes either one character insertion, deletion or one switch of corresponding characters.

I understand:

[edit_two_set.union(edit_one_letter(w, allow_switches)) for w in one]

is performing a list comprehension in which for every word in one we make one character edit and then take the union of the resulting set with the previous set.

I am mainly stuck at trying to understand what:

set.union(*[])

is doing? Thanks!

Upvotes: 2

Views: 331

Answers (1)

gis_grad_student
gis_grad_student

Reputation: 184

You can refer to this:

https://docs.python.org/3/library/stdtypes.html#frozenset.union

The list comprehension returns a list of sets.

set.union(*) would perform a union of the sets within the list and return a new set.

Upvotes: 2

Related Questions