suggestions, comments on an "is_subdict" test

Peter Otten __peter__ at
Fri Apr 22 11:06:03 EDT 2011

Zero Piraeus wrote:

> :
>>> I'd like to ask for comments or advice on a simple code for testing a
>>> "subdict", i.e. check whether all items of a given dictionary are
>>> present in a reference dictionary.
> Anything wrong with this?
> def is_subdict(test_dct, base_dct):
>     return test_dct <= base_dct and all(test_dct[k] == base_dct[k] for
> k in test_dct)

It may raise a KeyError.

More information about the Python-list mailing list