Expanding function based on array length
<p>I have the following code, which I would like to make responsive to the initial array that is passed to it (letters). Currently it can handle letters &lt;= 3, however I would like to make it expandable to n.</p>

<p>In this example, if the array only contains two entries ["a", " b"], it would trigger the second if statement. </p>

<p>How can I change this to have effectively infinite if statements, allowing an array of any size?</p>

<pre><code>import itertools
import numpy as np

#variable length
letters = ["a", " b", " c"]

increment = .1
d = 3
e = 3

#Calculate total possible combinations and create array
x=0
for p in itertools.product(range(d), repeat=e):
    x = x+1

variable = np.arange(x)
variable_s = [str(x) for x in variable]


x=0
#run loop based on array length.
for p in itertools.product(range(d), repeat=e):
    if len(letters) == 1:
        variable_s[x] = letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
    elif len(letters) == 2:
        variable_s[x] = (
            letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[1]+str(format(p[1]/(increment)**(-1.0),'.2f')).replace("0.", "")
        )
    elif len(letters) == 3:
        variable_s[x] = (
            letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[1]+str(format(p[1]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[2]+str(format(p[2]/(increment)**(-1.0),'.2f')).replace("0.", "")
        )
    x = x+1
variable_s
</code></pre>

<p>The output from the code above would be: 
['a00 b00 c00',
 'a00 b00 c10',
 'a00 b00 c20',
 'a00 b10 c00',
 'a00 b10 c10',
 'a00 b10 c20',
 'a00 b20 c00',
 'a00 b20 c10',
 'a00 b20 c20',
 'a10 b00 c00',
 'a10 b00 c10',
 'a10 b00 c20',
 'a10 b10 c00',
 'a10 b10 c10',
 'a10 b10 c20',
 'a10 b20 c00',
 'a10 b20 c10',
 'a10 b20 c20',
 'a20 b00 c00',
 'a20 b00 c10',
 'a20 b00 c20',
 'a20 b10 c00',
 'a20 b10 c10',
 'a20 b10 c20',
 'a20 b20 c00',
 'a20 b20 c10',
 'a20 b20 c20']</p>

<p>If letters = ["a", "b"]  the output would be :
['a00 b00',
 'a00 b00',
 'a00 b00',
 'a00 b10',
 'a00 b10',
 'a00 b10',
 'a00 b20',
 'a00 b20',
 'a00 b20',
 'a10 b00',
 'a10 b00',
 'a10 b00',
 'a10 b10',
 'a10 b10',
 'a10 b10',
 'a10 b20',
 'a10 b20',
 'a10 b20',
 'a20 b00',
 'a20 b00',
 'a20 b00',
 'a20 b10',
 'a20 b10',
 'a20 b10',
 'a20 b20',
 'a20 b20',
 'a20 b20']</p>

<p>If letters ["a", "b", "c", "d", "e", "f", "g"] the output would be:
['a00 b00 c00 d00 e00 f00 g00',
 'a00 b00 c00 d00 e00 f00 g10',... etc.</p>

<p>Here's the smallest change you could make - replace your <code>if</code> statement with:</p>

<pre><code>variable_s[x] = ''.join(
    letter+str(format(p_i/(increment)**(-1.0),'.2f')).replace("0.", "")
    for letter, p_i in zip(letters, p)
)
</code></pre>

<p>Other notes:</p>

<ul>
<li><code>p[1]/(increment)**(-1.0)</code> is better spelt <code>p[1] * increment</code></li>
<li><code>x=0; for p in itertools.product(range(d), repeat=e): x = x+1</code> is better spelt <code>x = d**e</code></li>
</ul>

python arrays itertools