Re: [Tutor] Adding to a dict through a for loop confusion.
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a dict through a for loop confusion.
On 17/05/16 23:56, Chris Kavanagh wrote: > 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}. . . > > Where as if you do NOT put the key in quotes, dict[key] or cart[item], this > basically means the dict has as many keys as you're iterating through. In > other words it assigns the cart_item as a key and a value, and saves them > all to the dict. > > Is that correct? Sort of, but not for the reasons you give. Putting the key in quotes makes it a literal string. That is, a fixed value, just like 2 or 5.5 or True. These are literal integer, float and boolean values respectively. So you could just as well have done: for item in cart_items: cart[2] = item And everything would be stored under a key of 2. Or for item in cart_items: cart[True] = item And everything would get stored on a key of True. The quotes turn the sequence of characters 'i','t','e','m' into the string value 'item'. The fact that the string is the same as the variable name in your for loop is completely coincidental. Python doesn't recognise them as in any way related. So in the first case the issue is that you stored your values in a single unchanging key. Whereas in the second loop you stored your values against a key which was a variable that changed in each iteration. The fact you used quotes is only significant in that quotes are what you use to create a literal string value. But it was the fact that it was a fixed value that made everything appear in the same place (and hence be overwritten), not the quotes per se. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a dict through a for loop confusion.
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}. . . Where as if you do NOT put the key in quotes, dict[key] or cart[item], this basically means the dict has as many keys as you're iterating through. In other words it assigns the cart_item as a key and a value, and saves them all to the dict. Is that correct? On Tue, May 17, 2016 at 5:01 AM,wrote: > On 17May2016 04:28, Chris Kavanagh wrote: > >> Could someone tell me why this different behavior occurs between these 2 >> code snippets, please. The 1st example has quotes around it ['item'] only >> adds the last item to the dict (cart). In the 2nd example the item does >> not >> have quotes around it [item] and every entry is added to the dict. >> >> Why? >> > [...] > >> # Example #1 >> cart_items = ['1','2','3','4','5'] >> cart = {} >> for item in cart_items: >>cart['item'] = item >> > > This for loop assigns the values '1','2','3','4','5' in succession to the > variable named "item". Then the body of the loop assigns that value (via > the variable "item") to the single dictionary slot with the fixed key with > string value 'item' i.e. always the same slot. And the last item is the > one kept. All the earlier assignments are overwritten by the later ones: > they happen, but are replaced. > > print cart >> #output >> {'item': 5} >> > > Which you see above. > > # Example #2 >> cart_items = ['1','2','3','4','5'] >> cart = {} >> for item in cart_items: >>cart[item] = item >> > > Here, the variable named "item" takes on the values as before, but the > diction slot chosen also comes form that variable. So each value ends up in > its own slot as your output shows. > > print cart >> # output >> {'1': '1', '3': '3', '2': '2', '5': '5', '4': '4'} >> > > The essential difference here is that in the first example the expression > for the slot in the dictionary is the expression: > > 'item' > > which is simply the fixed string 'item'. In the second example the > expression is: > > item > > which produces the current value stored in the variable "item". > > Cheers, > Cameron Simpson > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a dict through a for loop confusion.
On 17May2016 04:28, Chris Kavanaghwrote: Could someone tell me why this different behavior occurs between these 2 code snippets, please. The 1st example has quotes around it ['item'] only adds the last item to the dict (cart). In the 2nd example the item does not have quotes around it [item] and every entry is added to the dict. Why? [...] # Example #1 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart['item'] = item This for loop assigns the values '1','2','3','4','5' in succession to the variable named "item". Then the body of the loop assigns that value (via the variable "item") to the single dictionary slot with the fixed key with string value 'item' i.e. always the same slot. And the last item is the one kept. All the earlier assignments are overwritten by the later ones: they happen, but are replaced. print cart #output {'item': 5} Which you see above. # Example #2 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart[item] = item Here, the variable named "item" takes on the values as before, but the diction slot chosen also comes form that variable. So each value ends up in its own slot as your output shows. print cart # output {'1': '1', '3': '3', '2': '2', '5': '5', '4': '4'} The essential difference here is that in the first example the expression for the slot in the dictionary is the expression: 'item' which is simply the fixed string 'item'. In the second example the expression is: item which produces the current value stored in the variable "item". Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a dict through a for loop confusion.
On 17/05/16 09:28, Chris Kavanagh wrote: > # Example #1 > cart_items = ['1','2','3','4','5'] > > cart = {} > > for item in cart_items: > cart['item'] = item 'item' is a literal string. It never changes. So you keep overwriting the dict entry so that at the end of the loop the dict contains the last value associated with your literal key 'item' > #output > {'item': 5} > > > # Example #2 > cart_items = ['1','2','3','4','5'] > > cart = {} > > for item in cart_items: > cart[item] = item here you use the actual item from your data as both the key and the value. So you wind up with a dict containing a key/value pair for each value in your data list. > # output > {'1': '1', '3': '3', '2': '2', '5': '5', '4': '4'} That's not a very common requirement though, usually you would have different values from the keys. Maybe something like: for item in cart_items: cart[item] = int(item) which stores an integer value against the string representation: {'1': 1, '3': 3, '2': 2, '5': 5, '4': 4} Notice also that dicts don't usually print out in the same order as you created them. In fact the 'order' can even change over the life of your program, as you add/remove elements. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a dict through a for loop confusion.
Hello, Am 17.05.2016 um 10:28 schrieb Chris Kavanagh: Could someone tell me why this different behavior occurs between these 2 code snippets, please. The 1st example has quotes around it ['item'] only adds the last item to the dict (cart). In the 2nd example the item does not have quotes around it [item] and every entry is added to the dict. Why? # Example #1 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart['item'] = item print cart #output {'item': 5} Here you assign every item from your list to a dictionary entry with the same key 'item' - that's a string literal that hasn't got anything to do with the name item you give to each list element in turn. Because a dictionary can only contain one entry for each key the value of that entry cart['item'] is overwritten in every step through the loop. Only the result of the last step is kept. # Example #2 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart[item] = item print cart # output {'1': '1', '3': '3', '2': '2', '5': '5', '4': '4'} Here you take the value from the list cart_items, called item, for the key and for the value of the dictionary entry. So every entry gets a different key and your dictionary has as many entries as the list has elements. But did you really want to get a dictionary with the same values for key and value? HTH Sibylle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Adding to a dict through a for loop confusion.
Could someone tell me why this different behavior occurs between these 2 code snippets, please. The 1st example has quotes around it ['item'] only adds the last item to the dict (cart). In the 2nd example the item does not have quotes around it [item] and every entry is added to the dict. Why? # Example #1 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart['item'] = item print cart #output {'item': 5} # Example #2 cart_items = ['1','2','3','4','5'] cart = {} for item in cart_items: cart[item] = item print cart # output {'1': '1', '3': '3', '2': '2', '5': '5', '4': '4'} Thanks you for the help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor