Testing for an empty dictionary in Python

Arnaud Delobelle arnodel at googlemail.com
Sun Mar 23 16:08:41 EDT 2008


On Mar 23, 5:45 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> John Nagle <na... at animats.com> writes:
> >    What's the cheapest way to test for an empty dictionary in Python?
>
> >    if len(dict.keys() > 0) :
>
> I like to think len(dict) is constant time but I haven't checked the code.
> Same for bool(dict) (which is what you get when you run "if dict: ...").

It has to be constant time as it is a lower bound for inserts (which
average to constant time).

--
Arnaud




More information about the Python-list mailing list