A little morning puzzle

N

Neal Becker

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?
 
J

Jussi Piitulainen

Neal said:
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) }
 
P

Paul Rubin

Neal Becker said:
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))
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top