RE: Dict comprehension help

Discussion in 'Python' started by Joseph L. Casale, Dec 6, 2012.

  1. >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
     
    Joseph L. Casale, Dec 6, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vedran Furac(
    Replies:
    4
    Views:
    356
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
  2. Joseph L. Casale

    Dict comprehension help

    Joseph L. Casale, Dec 6, 2012, in forum: Python
    Replies:
    0
    Views:
    136
    Joseph L. Casale
    Dec 6, 2012
  3. Ian Kelly

    Re: Dict comprehension help

    Ian Kelly, Dec 6, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    Ian Kelly
    Dec 6, 2012
  4. Joseph L. Casale

    RE: Dict comprehension help

    Joseph L. Casale, Dec 6, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    Joseph L. Casale
    Dec 6, 2012
  5. Peter Otten

    RE: Dict comprehension help

    Peter Otten, Dec 6, 2012, in forum: Python
    Replies:
    0
    Views:
    167
    Peter Otten
    Dec 6, 2012
Loading...

Share This Page