title: In Python, fastest way to create all possible combinations of replacing up to N positions in a list with a character
body: <p>I have a specific use case where I need to create every combination of a list containing strings, replacing up to N elements with a specific character. Currently I am generating every single possible combination, and then filtering for which combinations have less than N elements replaced, which is not very effective.</p>
  <pre><code> from itertools import product,repeat
  
   l = [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;]
   n = 2
  
   l_combs = list(set(product(*zip(l, repeat('')))))
  </code></pre>
  <p><code>l_combs</code> now contains
  <code>[('A', 'B', 'C'), ('A', 'B', ''), ('A', '', ''), ('', '', 'C'), ('', 'B', 'C'), ('', 'B', ''), ('', '', ''), ('A', '', 'C')]</code></p>
  <pre><code>l_wanted= [x for x in l_combs if x.count('') &lt;= n)]
  </code></pre>
  <p><code>l_wanted</code> now contains
  <code>[('A', 'B', 'C'), ('A', 'B', ''), ('A', '', ''), ('', '', 'C'), ('', 'B', 'C'), ('', 'B', ''), ('A', '', 'C')]</code></p>
  <p>And if the list <code>l</code> contains many elements, this can be very in efficient.</p>
  <p>What would be an effective way to generate all combinations replacing elements up to N times in a list of generic length?</p>