A little morning puzzle

Discussion in 'Python' started by Neal Becker, Sep 19, 2012.

  1. Neal Becker

    Neal Becker Guest

    I have a list of dictionaries. They all have the same keys. I want to find the
    set of keys where all the dictionaries have the same values. Suggestions?
     
    Neal Becker, Sep 19, 2012
    #1
    1. Advertising

  2. Neal Becker writes:

    > I have a list of dictionaries. They all have the same keys. I want
    > to find the set of keys where all the dictionaries have the same
    > values. Suggestions?


    Literally-ish:
    { key for key, val in ds[0].items() if all(val == d[key] for d in ds) }
     
    Jussi Piitulainen, Sep 19, 2012
    #2
    1. Advertising

  3. Neal Becker

    Paul Rubin Guest

    Neal Becker <> writes:
    > I have a list of dictionaries. They all have the same keys. I want to find the
    > set of keys where all the dictionaries have the same values. Suggestions?


    Untested, and uses a few more comparisons than necessary:

    # ds = [dict1, dict2 ... ]

    d0 = ds[0]
    ks = set(k for k in d0 if all(d[k]==d0[k] for d in ds))
     
    Paul Rubin, Sep 19, 2012
    #3
    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. Peter Otten

    Re: A little morning puzzle

    Peter Otten, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    187
    Peter Otten
    Sep 19, 2012
  2. Dwight Hutto

    Re: A little morning puzzle

    Dwight Hutto, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    161
    Dwight Hutto
    Sep 19, 2012
  3. Peter Otten

    Re: A little morning puzzle

    Peter Otten, Sep 19, 2012, in forum: Python
    Replies:
    9
    Views:
    180
    Prasad, Ramit
    Sep 27, 2012
  4. Antoon Pardon

    Re: A little morning puzzle

    Antoon Pardon, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    177
    Antoon Pardon
    Sep 19, 2012
  5. Dwight Hutto

    Re: A little morning puzzle

    Dwight Hutto, Sep 19, 2012, in forum: Python
    Replies:
    0
    Views:
    168
    Dwight Hutto
    Sep 19, 2012
Loading...

Share This Page