Python 2D Dynamic List Error
<p>I am new to Python.</p>

<pre><code>     for row in reader:

            if (int(row[0]) &lt;= nextWeek):
                  y[i].append(row[1])
            if (int(row[0]) &gt; nextWeek):
                    i = i + 1
                    y[i].append(row[1])
</code></pre>

<p>These are the parts of the code , here I need to acheive this kind of things in Python , What is the <strong>correct way of declaring y</strong> ?</p>

<p>Here I used <strong>y = [ [ ] ]</strong> , But I need the correct type for <strong>y</strong> something like basically 2D Dynamic List Declaration / Initialization example ?</p>

<p>I think what you are searching for is a defaultdict.
Lets say you've got:</p>

<pre><code>from collections import defaultdict
y = defaultdict(list)
(...)
for row in reader:
        if (int(row[0]) &lt;= nextWeek):
              y[i].append(row[1])
        if (int(row[0]) &gt; nextWeek):
                i = i + 1
                y[i].append(row[1])
</code></pre>

<p>In this case, the defaultdict will store the "i" (index) as a key, where the value will be a list by default. (and as such it is easy to extend the list, when not-knowing how many of them should exist). </p>

python matplotlib