Code Explaination: Spelling correction code

Steven Bethard steven.bethard at
Thu Apr 12 05:27:42 CEST 2007

Drew wrote:
> I recently saw this website:
> All the code makes sense to me save one line:
> def known_edits2(word):
>     return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in

This is the same as:

     result = set()
     for e1 in edits1(word):
         for e2 in edits1(e1):
             if e2 in NWORDS:
     return result

The thing between the ``set(`` and ``)`` is called a generator 
comprehension if you'd like to look into it further.


More information about the Python-list mailing list