[Tutor] Adding to a dict through a for loop confusion.

Steven D'Aprano steve at pearwood.info
Tue May 17 21:54:09 EDT 2016


On Tue, May 17, 2016 at 06:56:40PM -0400, Chris Kavanagh wrote:
> Thank you so much for the help, and the example!
> 
> So, by putting quotes around a dict key, like so dict["key"] or in my case
> cart["item"] this makes the dict have ONE key. The loop assigns the
> cart_items to this ONE key until the end of the loop, and I'm left with
> {'item': 5}. . .

Chris, you're still thinking about this the wrong way. Let's look at a 
slightly different example which hopefully will give you some better 
perspective. I'm going to use a list, not a dict.


I hope it is obvious what is going on here:

py> mylist = [100, 200, 300]
py> for i in (0, 1, 2):
...     mylist[i] = 1000 + i
...
py> mylist
[1000, 1001, 1002]

If we "unroll" the for-loop and turn it into individual lines of code, 
we're effectively executing this:

    i = 0
    mylist[i] = 1000 + i  # here, i==0 so mylist[0] = 1000
    i = 1
    mylist[i] = 1000 + i  # here, i==1 so mylist[1] = 1001
    i = 2
    mylist[i] = 1000 + i  # here, i==2 so mylist[2] = 1002


Any questions?

Now, let's look at this one instead:

py> mylist = [100, 200, 300]
py> for i in (0, 1, 2):
...     mylist[0] = 1000 + i
...
py> mylist
[1004, 200, 300]

Let's unroll the for-loop:

    i = 0
    mylist[0] = 1000 + i
    i = 1
    mylist[0] = 1000 + i
    i = 2
    mylist[0] = 1000 + i


So far, all of this should hopefully be obvious. If you keep assigning 
to mylist[0] each time, only the first item of the list will be changed. 
In the first case, you assigned to a different item each time through 
the loop, because you wrote mylist[i] and i changed each time. In the 
second case, you assigned to mylist[0] each time.

There's nothing fancy or tricky going on here. The difference is 100% 
and entirely because of the difference between writing the VARIABLE i 
and writing the CONSTANT 0.

And it is the same with dict keys. If you loop over the dict:

cart_items = ['one', 'two', 'three']
for item in cart_items:
    cart[item] = item

you are assigning to a different key each time, because you have written 
the VARIABLE item, which changes each time through the loop. If we 
unroll the loop again, we see:

    item = 'one'
    cart[item] = item
    item = 'two'
    cart[item] = item
    item = 'three'
    cart[item] = item

But when you do this:

for item in cart_items:
    cart['item'] = something

you have written the CONSTANT 'item', which is always exactly the same 
thing: a four letter word starting with 'i', ending with 'm', with 'te' 
in the middle. It is nothing but an unfortunately coincidence that the 
value of this constant, 'item', matches the name of the variable item. 
Let's get rid of the coincidence:

for item in cart_items:
    cart['word'] = something

Unrolling the loop gives us:

    item = 'one'
    cart['word'] = item
    item = 'two'
    cart['word'] = item
    item = 'three'
    cart['word'] = item


And now hopefully it is clear what is going on! If you write this:

cart = {}
cart['word'] = 'one'
cart['word'] = 'two'
cart['word'] = 'three'

it should be clear why the end result is {'word': 'three'} rather than 
{'one': 'one', 'two': 'two', 'three': 'three'}.



-- 
Steve


More information about the Tutor mailing list