create a dynamic two dimensional array in python (loop)
<p>i am trying to create a two-dimensional array in python..In my php i have this code: </p>

<pre><code>$i = 1;
$arr= array();
        foreach($mes as $res){

            $arr[$i]-&gt;type = $res-&gt;item;
            $arr[$i]-&gt;action = $res-&gt;title;
$i++
}
</code></pre>

<p>how can i make that code in python? i need to built that code dynamically for the foreach loop as you can see in the code above..i need to assign the values..arr[1]['type'] = 'blabla', arr[1]['action] = 'blabla'..i hope you understand what i am trying to do</p>

<p>i have done this but doesnt work:</p>

<pre><code>i = 1

for res in mes:

    fa = [i]

    fa[i].append(['type'])

    fa[i]['type'].append(res['item'])
</code></pre>

<p>A <em>list comprehension</em> with nested dictionaries is a simple way to do this:</p>

<pre><code>arr = [{'type': res['item'], 'action': res['title']} for res in mes]
</code></pre>

<hr>

<p>The first item (and others similarly by changing the index) in <code>arr</code> can then be accessed with:</p>

<pre><code>arr[0]['type'] # or arr[0]['action'] 
#   ^ indices of lists in Python start from zero
</code></pre>

python