title: Yielding all sub-anagrams from a string
body: <p>I am trying to extend the code from the top answer here:</p>
  <p><a href="https://stackoverflow.com/questions/44852201/find-all-the-possible-n-length-anagrams-fast-alternatives">Find all the possible N-length anagrams - fast alternatives</a></p>
  <p>I need to do the same thing, but to yield all sub-anagrams (without repetition).</p>
  <p>Most questions on this subject treat only input strings like <code>&quot;ABCD&quot;</code> where the characters are all unique.</p>
  <p><strong>EXAMPLE</strong></p>
  <p>Input = <code>&quot;abb&quot;</code></p>
  <p>Expected output is an iterator that yields from</p>
  <p><code>[&quot;a&quot;, &quot;b&quot;, &quot;ab&quot;, &quot;ba&quot;, &quot;bb&quot;, &quot;abb&quot;, &quot;bab&quot;, &quot;bba&quot;]</code></p>
  <p>So, for instance, <code>&quot;bba&quot;</code> isn't yielded twice as a result of there being two <code>&quot;b&quot;</code> occurences in the input string.</p>
  <p>The following code does what I want:</p>
  <pre><code>from itertools import permutations, chain
  from typing import Iterator
  
  def shuffler(word: str) -&gt; Iterator[str]:
      yield from set(chain(
          *map(
              lambda x:
              permutations(word, x), range(1, len(word) + 1)
          )
      ))
  </code></pre>
  <p>This code first creates a set, however. As I will be using this function many thousands of times, the code will be slower than if a &quot;pure&quot; iterator were returned from the function.</p>