How do I access each element in list in for-loop operations?
<p>I am looping through this list and don't understand why <code>print(d)</code> returns each number in <code>seed</code> but assigning <code>i["seed"] = d</code> assigns the last element of <code>seed</code>.</p>

<p>How do I access each element in <code>seed</code> for operations other than <code>print()</code>?</p>

<pre><code>res = []
seed = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = {}
for d in seed:
    print(d)
    i["seed"] = d
    res.append(i)
print(res)
</code></pre>

<p>Thanks!</p>

<p>The problem is where you are defining i. I <em>think</em> you intended to have a dictionary object named i that has  single attribute named "seed". and you want to add those dictionaries to res.</p>

<p>In actual fact you only have one dictionary called i and you just update the value in it every time through the loop. </p>

<p>try this:</p>

<pre><code>res = []
seed = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for d in seed:
    i = {}
    print(d)
    i["seed"] = d
    res.append(i)
print(res)
</code></pre>

<p>That will create a new instance of i for each loop, and you should get the result you are looking for.</p>

python for-loop