[Tutor] if value in list of dictionaries

Emile van Sebille emile at fenx.com
Mon Sep 27 23:39:52 CEST 2010


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

		



More information about the Tutor mailing list