[Tutor] if value in list of dictionaries

Norman Khine norman at khine.net
Tue Sep 28 16:12:50 CEST 2010


thanks for the reply, i think i have it now, perhaps it could be done better

http://pastie.org/1186545

On Tue, Sep 28, 2010 at 2:56 PM, Emile van Sebille <emile at fenx.com> wrote:
>  Hi Norman,
>
> Read my reply again -- that's the second question I answered.
>
> Emile
>
>
> On 9/28/2010 12:56 AM Norman Khine said...
>>
>> thanks for the reply. i should have been more specific in my question ;)
>>
>> the order in which 'other' is listed is not always the last item of
>> the list as it is dependent on where in the CSV file it is included.
>>
>> what i was trying to do is to take create the list of dictionary items
>> and then find the item which has name=='other' and then put this as
>> the last item in the list.
>>
>> so from this list http://pastie.org/1185974 how do i select the item
>> name == 'other' and put it at the end of the list?
>>
>> On Mon, Sep 27, 2010 at 11:39 PM, Emile van Sebille<emile at fenx.com>
>>  wrote:
>>>
>>> On 9/27/2010 1:22 PM Norman Khine said...
>>>
>>>> what is the correct way to ensure that {'industry': 'travel', 'name':
>>>> 'other','value': MSG(u"Other")} is always added to the end of this
>>>> list after all the items have been sorted?
>>>>
>>>> here is the code which returns this list:
>>>
>>>   options.sort(key=itemgetter('name'))
>>>   return options
>>>
>>> So, to answer the question you ask above, you can do:
>>>
>>>   options.sort(key=itemgetter('name'))
>>>   options.append({'industry':'travel',
>>>      'name':'other','value':MSG(u"Other")}
>>>   return options
>>>
>>> But I don't think that's the question you're looking to get answered.
>>>
>>> I think you want "other" to be found only at the end and not elsewhere.
>>>
>>> Then you might try excluding other from options allowing the above to
>>> append
>>> it to the end:
>>>
>>> for index, row in enumerate(topics.get_rows()):
>>>    if row[0] != 'other':
>>>        options.append({'name': row[0], 'value': MSG(row[1])})
>>>
>>> HTH,
>>>
>>> Emile
>>>
>>>
>>>
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>
>



-- 
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] )


More information about the Tutor mailing list