newbie question - python lists

Rami Chowdhury rami.chowdhury at gmail.com
Fri Nov 6 12:12:32 EST 2009


On Fri, 06 Nov 2009 08:49:52 -0800, lee <san82moon at gmail.com> wrote:

> On Nov 6, 7:48 pm, Jim <jim.heffe... at gmail.com> wrote:
>> On Nov 6, 9:15 am, lee <san82m... at gmail.com> wrote:
>>
>> > can anyone point wer am erroring.
>>
>> I'm not sure what you are trying to do, but it is odd, isn't it, that
>> you never refer to brain in the "for brain in brains:" loop?  I think
>> you are mixing i and brain somehow.
>>
>> Jim
>
> ok let me make it clear,
>
> brains = ['1','2']
> for brain in brains:
>     row['item'] = brain
>     items.append(row)
>     print items
>
> This produces
> [{'item': '1'}]
> [{'item': '2'}, {'item': '2'}]
> but i want
> [{'item': '1'}]
> [{'item': '1'}, {'item': '2'}]
>

Lee,

When you do
>     row['item'] = brain
you are actually modifying the existing dictionary object called 'row' --  
and I get the impression this is not what you want to do.

I'd suggest creating a new dictionary on each pass, which should give you  
the desired behavior.

-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list