I want to check if a dictionary contains the exact same keys as in a list of keys (no more, no less). I'm currently using all() and a length check to do this. Is there a better way? Thanks!
d = {'1': 'one', '3': 'three', '2': 'two'}
key_list = ['1', '2', '3']
all(col in d for col in key_list) and len(d) == 3
True
What about
set(d) == set(key_list)
set(d) is equals to set(d.keys()) as @gmds pointed out
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With