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

2016-05-17 Thread Steven D'Aprano
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

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

2016-05-17 Thread Alan Gauld via Tutor
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

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

2016-05-17 Thread Chris Kavanagh
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

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

2016-05-17 Thread cs
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

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

2016-05-17 Thread Alan Gauld via Tutor
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

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

2016-05-17 Thread Sibylle Koczian
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

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

2016-05-17 Thread 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?