Testing for an empty dictionary in Python

Paddy paddy3118 at googlemail.com
Sun Mar 23 14:02:40 EDT 2008


On Mar 23, 4:14 pm, Paddy <paddy3... at googlemail.com> wrote:
> 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 own container types follow the same
> convention too.
>
> - Paddy.

I missed out *empty* didn't I.
Its false for empty container types.

- Paddy.



More information about the Python-list mailing list