Hello again, Here is my full attempt, can you please tell me if it is OK and if there should be any improvements
>>> ws_industry = ('it', 'science') >>> code = ws_industry[0] >>> active = [] >>> industries = [{'code': 'it', 'name': 'Information Technology'}, {'code': >>> 'science', 'name': 'Science'}, {'code': 'biology', 'name': 'Biology'}] >>> for industry in industries: if industry['code'] in ws_industry: active.append({ 'code': industry['code'], 'name': industry['name'], 'isdefault': code == industry['code']}) >>># Not active industry >>> not_active = [x for x in industries if x['code'] not in ws_industry ] >>>not_active.sort(lambda x, y: cmp(x['name'], y['name'])) >>> return [active, non-active] Line [6] i was not too sure if it could be written better or more efficient. Basically I have a big list of industries and I want to verify for each member whether they are active or not active. The industry list of dictionaries will stay fixed at about 25 items, whereas the ws_industry tuple will change depending. I have to go through about 20,000 items. -------- Original Message -------- From: davidwil...@safe-mail.net Apparently from: tutor-bounces+davidwilson=safe-mail....@python.org To: tutor@python.org Subject: Re: [Tutor] dictionaries help Date: Thu, 23 Jul 2009 18:14:01 -0400 > thank you for all your answers > > > -------- Original Message -------- > From: Norman Khine <nor...@khine.net> > Apparently from: tutor-bounces+davidwilson=safe-mail....@python.org > To: tutor@python.org > Subject: Re: [Tutor] dictionaries help > Date: Thu, 23 Jul 2009 21:59:01 +0100 > > > Also you can use list comprehension > > > > In [1]: my_lst = [{'code': 'aaa', 'name': 'a name'}, {'code': 'bbb', > > 'name': 'b name'}] > > > > In [2]: my_code = 'aaa' > > > > In [3]: print [d for d in my_lst if d['code'] == my_code] > > ------> print([d for d in my_lst if d['code'] == my_code]) > > [{'code': 'aaa', 'name': 'a name'}] > > > > In [4]: > > > > > > On Thu, Jul 23, 2009 at 6:04 PM, Alan Gauld<alan.ga...@btinternet.com> > > wrote: > > > > > > "Alan Gauld" <alan.ga...@btinternet.com> wrote > > > Oops! > > > Should be: > > > > > >> def findDict(value, dictList): > > >> for dct in dictList: > > >> if dct.get('code', '') == my_code > > > > > > if dct.get('code', '') == value > > > > > >> return dct > > > > > > > > >> HTH, > > >> > > >> > > >> -- > > >> Alan Gauld > > >> Author of the Learn to Program web site > > >> http://www.alan-g.me.uk/ > > > > > > _______________________________________________ > > > Tutor maillist - tu...@python.org > > > http://mail.python.org/mailman/listinfo/tutor > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor