How to make an One-Liner list with multiple variables in Python?
<p>I was interested whether I can define <em>ascii_combinations</em> list in one line and not using the loop I used in the following example...</p>

<pre><code>ascii_printable = [chr(count) for count in range(32, 127)]
ascii_combinations = []

for x1 in ascii_printable:
    for x2 in ascii_printable:
        for x3 in ascii_printable:
            ascii_combinations.append(x1 + x2 + x3)
</code></pre>

<p>I wanted to create a list of all possible 3 characters long combinations using 95 single-character ASCII characters. I made it work using this code but as I managed to shorten <em>ascii_printable</em> into One-Liner, I was interested whether I can do the same thing with the other list.</p>

<p>You might use <code>itertools.product</code> as</p>

<pre><code>from itertools import product

ascii_printable = [chr(count) for count in range(32, 127)]
ascii_combinations = [x1 + x2 + x3 for x1, x2, x3 in product(ascii_printable, repeat=3)]
</code></pre>

<p>Also <code>chr(count) for count in range(32, 127)</code> is way less clear than</p>

<pre><code>from itertools import product
from string import printable

ascii_combinations = [x1 + x2 + x3 for x1, x2, x3 in product(printable[:-5], repeat=3)]
</code></pre>

<p>Lastly, comprehensions are nice but sometimes it's easier to think, imho, in terms of maps of iterables, thus</p>

<pre><code>from itertools import product
from string import printable
list(map(''.join, product(printable[:-5], repeat=3)))
</code></pre>

python