newbie question - python lists

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


On Fri, 06 Nov 2009 09:19:00 -0800, lee <san82moon at gmail.com> wrote:

> On Nov 6, 9:47 pm, lee <san82m... 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'}]
>>
>> if i do items.append(brain), it gives,
>> ['1', '2']
>> but i want dictionary inside list.
>> @Jon - Yes i want single item dict's
>> @Robert - i use python 2.4 .
>>
>> Thanks
>> Lee.
>
> i got it solved ,
>
> items = []
> for brain in brains:
>        dict = {'itemnos': 'item_0' + brain + 's'}
>        items.append(dict)
> print(items)
>

Glad to see you solved it! A couple of minor considerations:
	- I would suggest you use a variable name other than 'dict', as that  
shadows the built-in 'dict' type
	- I would suggest using the idiom 'item_0%ss' % brain rather than  
'item_0' + brain + 's', in case at some point you want to change the type  
of 'brain'


-- 
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