using variable name within list as string for file save python
<p>I have a python list which contains 4 elements, each of which is a time-series dataset. 
e.g., </p>

<pre><code>full_set = [Apple_px, Banana_px, Celery_px]
</code></pre>

<p>I am charting them via matplotlib and I want to save the charts individually using the variable names. </p>

<pre><code>for n in full_set:

*perform analysis

    plt.savefig("Chart_{}.png".format(n))
</code></pre>

<p>The ideal output would have Chart_Apple_px, Chart_Banana_px, Chart_Celery_px as the chart names.</p>

<p>In Python names and values are two very different things, stored and dealt with in different ways - and more practically one value can have multiple names assigned to it.</p>

<p>So, instead of trying to get the name of some data value, use a tuple where you assign a title to the dataset in your list of datasets, something like this:</p>

<pre><code>full_set = [('Chart_Apple_px', Apple_px), ('Chart_Banana_px', Banana_px)]

for chart_name, dataset in full_set:
   # do your calculations on the dataset
   plt.savefig("{}.png".format(chart_name))
</code></pre>

python matplotlib