Indexing variables and adding month to variable name in python3.5
<p>I'm trying to set up a list of variables to work with in a linear programming problem. For this I'd like to work with some index values to make the code significantly shorter and easier to read. I'd tried something as:</p>

<pre><code>from datetime import *

months = ["Unknown", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

d={}

for x in range(1,13):
    d["string{0}".format(x)]=("Production in months[d]")

print(d)
</code></pre>

<p>This returns me a list:</p>

<pre><code>{'string7': 'Production in months[d]', 'string8': 'Production in months[d]', 'string2': 'Production in months[d]', 'string9': 'Production in months[d]', 'string11': 'Production in months[d]', 'string6': 'Production in months[d]', 'string12': 'Production in months[d]', 'string3': 'Production in months[d]', 'string10': 'Production in months[d]', 'string4': 'Production in months[d]', 'string1': 'Production in months[d]', 'string5': 'Production in months[d]'}
</code></pre>

<p>I would like to have the name of the month where months[d] is printed, corresponding to the index number i in 'string[i]'.</p>

<p>That's simple, and you have already done it once in your code!</p>

<pre><code>for x in range(1,13):
    d["string{0}".format(x)]="Production in {}".format(months[x])

for key, value in d.items():
    print(key, value)
</code></pre>

<p>Output:</p>

<pre><code>string5 Production in May
string4 Production in April
string1 Production in January
string2 Production in February
string11 Production in November
string9 Production in September
string7 Production in July
string8 Production in August
string10 Production in October
string3 Production in March
string12 Production in December
string6 Production in June
</code></pre>

<p>Note that the order maybe different. Also, when you don't specify any position in the placeholders for <code>format()</code> arguments as in <code>"Production in {}".format(months[x])</code>, the arguments are inserted in the order they are supplied.</p>

python python-3.x