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. Advertisements

  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. Advertisements

  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. Advertisements

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. clintonG
    Replies:
    2
    Views:
    355
    Steve C. Orr [MVP, MCSD]
    Apr 1, 2005
  2. rosyn

    good morning

    rosyn, Feb 6, 2006, in forum: Java
    Replies:
    8
    Views:
    712
    megagurka
    Feb 7, 2006
  3. prassu

    Good morning...

    prassu, Jan 22, 2007, in forum: C Programming
    Replies:
    5
    Views:
    608
    Keith Thompson
    Jan 22, 2007
  4. Replies:
    0
    Views:
    431
  5. castironpi

    morning in Python

    castironpi, May 16, 2008, in forum: Python
    Replies:
    17
    Views:
    688
    inhahe
    May 18, 2008
  6. ex
    Replies:
    41
    Views:
    518
    Peña, Botp
    Jun 23, 2008
  7. Peter Otten

    Re: A little morning puzzle

    Peter Otten, Sep 19, 2012, in forum: Python
    Replies:
    9
    Views:
    248
    Prasad, Ramit
    Sep 27, 2012
  8. Stefan Ram
    Replies:
    7
    Views:
    300
    Daniel Pitts
    Dec 19, 2013
Loading...