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 issue is there is only one object <code>i</code>, and you're replacing its value every time in the loop.  By the last iteration your (one instance) of i has its value set to the last item you assigned it to.  Each time you append to <code>res</code>, you're basically appending the a pointer to the same object (so you end up with 9 pointers pointing to the same object when your loop is finished).</p>

<pre><code>res = []
seed = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = {}
for d in seed:
    print(d)
    i["seed"] = d  # this gets replaced every time through your loop!
    res.append(i.copy())  # need to copy `i`, or else we're updating the same instance
print(res)
</code></pre>

python for-loop