Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34399

RE: Dict comprehension help

From "Joseph L. Casale" <jcasale@activenetwerx.com>
Subject RE: Dict comprehension help
Date 2012-12-06 15:32 +0000
References <92097A6A775D5147B1078E3F15430B92349092E6@prato.activenetwerx.local> <CALwzidmPjPLJR+GyYsoefCiTdJ4YpfN5Vb+LDXVAWWMWFbRPrA@mail.gmail.com> <k9psu6$2et$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.568.1354808075.29569.python-list@python.org> (permalink)

Show all headers | View raw


>You could put the loop into a helper function, but if you are looping
>through the same my_list more than once why not build a lookup table
>
>my_dict = {d["key"]: d for d in my_list}
>
>and then find the required dict with
>
>my_dict[value]

I suppose, what I failed to clarify was that for each list of dicts, I may
only extract out one (maybe two) dicts, but I have several to do this for.

The tidy one-liners make for populating the vars in __init__ nice and clean.

Much appreciated guys, thanks!
jlc

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

RE: Dict comprehension help "Joseph L. Casale" <jcasale@activenetwerx.com> - 2012-12-06 15:32 +0000

csiph-web