Python set update with iterable
Question: What I have at hand is a text file of decent size (~ 23MB). I am reading the
file line by line and extracting a few words from each line based on some
external criteria. For the sake of this example, let's say each line contains
at least half a dozen tab-separated values and I am picking up all except the
first and the last.

I want to output the set of _unique_ words obtained this way, so obviously I
want to store the extracted words in a `set`. Also, since `set.update(other)`
is significantly faster than looping through `other` and adding one word at a
time with `set.add(elem)`, I tried doing this:

    
    
    all_words = set()
    with open(my_tsv_file) as tsv_file:
        for line in tsv_file:
            wordlist = based_on_some_criteria(line)    # this is a list, not a set
            all_words.update(wordlist)
    

And this works fine. But when I replaced `all_words.update(wordlist)` with
`all_words |= wordlist`, I got the following error:

    
    
    TypeError: unsupported operand type(s) for |=: 'set' and 'list'
    

From the documentation, I understand that `update` and `|=` are equivalent.
Further, since `|=` is supposed to accept any iterable, I also confirmed by
doing this:

    
    
    import collections
    isinstance(wordlist, collections.Iterable)        # returns True
    

Why is the first approach using `set.update` working, but not the second one
using `|=`?


Answer: > From the documentation, I understand that update and |= are equivalent.
> Further, since |= is supposed to accept any iterable...

From the documentation:

> Note, the non-operator versions of the `update()`, `intersection_update()`,
> `difference_update()`, and `symmetric_difference_update()` methods will
> accept any iterable as an argument.

The documentation does not seem to agree with your understanding.

