Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Steven D'Aprano
On Wed, 29 Sep 2010 01:41:22 am Peter Otten wrote: > You should never iterate over a list or dictionary and add or remove > items to it at the same time. That is a recipe for disaster even if > it doesn't fail explicitly* [...] > (*) I'll leave it to Steven D'Aprano to add the fine print ;) I'l

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Emile van Sebille
On 9/28/2010 11:13 AM Norman Khine said... ok, great. one thing i wanted to ask is how could i extend the class so that i can just change the name of the csv file? Python provides for instance initialization with a class __init__ method, so you could modify your class as follows: def sort

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
ok, great. one thing i wanted to ask is how could i extend the class so that i can just change the name of the csv file? On Tue, Sep 28, 2010 at 6:53 PM, Emile van Sebille wrote: > On 9/28/2010 9:37 AM Norman Khine said... >> >> thank you, here is the updated version: >> >> http://pastie.org/118

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Emile van Sebille
On 9/28/2010 9:37 AM Norman Khine said... thank you, here is the updated version: http://pastie.org/1186860 The only obvious redundancy is the duplicated sort of options just before the return. You only need the newer sort_key based one. Emile ___

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
thank you, here is the updated version: http://pastie.org/1186860 On Tue, Sep 28, 2010 at 5:50 PM, Emile van Sebille wrote: > On 9/28/2010 7:12 AM Norman Khine said... >> >> thanks for the reply, i think i have it now, perhaps it could be done >> better >> > > I think I'd use a helper function t

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Emile van Sebille
On 9/28/2010 7:12 AM Norman Khine said... thanks for the reply, i think i have it now, perhaps it could be done better I think I'd use a helper function to sort: def sortOtherToEnd(val): if val['name'] == 'other: return '' return val['name'] #then sort it topics.sort(key=sortOth

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Peter Otten
Norman Khine wrote: > thanks for the reply, i think i have it now, perhaps it could be done > better > >>> topics.sort(key=itemgetter('name')) > >>> for i, t in enumerate(topics): > ... for (k, v) in t.iteritems(): > ... if v == 'other': > ... topics.append(top

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
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 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

Re: [Tutor] if value in list of dictionaries

2010-09-28 Thread Norman Khine
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 th

Re: [Tutor] if value in list of dictionaries

2010-09-27 Thread Emile van Sebille
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=itemgette