Testing for an empty dictionary in Python

Paddy paddy3118 at googlemail.com
Sun Mar 23 12:14:47 EDT 2008


On Mar 23, 3:53 pm, John Nagle <na... at animats.com> wrote:
>    What's the cheapest way to test for an empty dictionary in Python?
>
>         if len(dict.keys() > 0) :
>
> is expensive for large dictionaries, and makes loops O(N^2).
>
>                                         John Nagle

As others have stated, if <container object>: is false for built-in
container types such as  dicts, lists, sets, tuples,...
Its nice to make any of your owh container types follow the same
convention too.

- Paddy.



More information about the Python-list mailing list