Re: Testing for an empty dictionary in Python - documented?

2008-03-23 Thread John Nagle
Bryan Olson wrote: > D'Arcy J.M. Cain wrote: >> John Nagle wrote: >>>What's the cheapest way to test for an empty dictionary in Python? > >> Try this: >> >> if dict: > > D'Arcy is right; that's the way to go. I'll add that 'dict' is the name > of the built-in class, so an instance is us

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Bryan Olson
D'Arcy J.M. Cain wrote: > John Nagle wrote: >>What's the cheapest way to test for an empty dictionary in Python? > Try this: > > if dict: D'Arcy is right; that's the way to go. I'll add that 'dict' is the name of the built-in class, so an instance is usually best named something else.

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Steven D'Aprano
On Sun, 23 Mar 2008 18:56:51 -0700, John Machin wrote: >> Python knows the truth value of built-in types like dicts without >> actually converting them to bools, or for that matter calling __len__ >> or __nonzero__ on them. > > What the 2.5.1 interpreter does is call PyObject_IsTrue, which checks

Re: Testing for an empty dictionary in Python

2008-03-23 Thread John Machin
On Mar 24, 11:32 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Sun, 23 Mar 2008 10:45:38 -0700, Paul Rubin wrote: > > John Nagle <[EMAIL PROTECTED]> writes: > >>What's the cheapest way to test for an empty dictionary in Python? > > >>if len(dict.keys() > 0) : >

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Steven D'Aprano
On Sun, 23 Mar 2008 10:45:38 -0700, Paul Rubin wrote: > John Nagle <[EMAIL PROTECTED]> 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 b

Re: Testing for an empty dictionary in Python

2008-03-23 Thread John Machin
On Mar 24, 2:53 am, John Nagle <[EMAIL PROTECTED]> wrote: >What's the cheapest way to test for an empty dictionary in Python? > > if len(dict.keys() > 0) : TypeError: object of type 'bool' has no len() I presume you meant if len(dict.keys()) > 0: > > is expensive for large dictio

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Arnaud Delobelle
On Mar 23, 5:45 pm, Paul Rubin wrote: > John Nagle <[EMAIL PROTECTED]> 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. > Sam

Re: Testing for an empty dictionary in Python

2008-03-23 Thread John Nagle
Brian Lane wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > John Nagle wrote: >>What's the cheapest way to test for an empty dictionary in Python? >> >> if len(dict.keys()) : >> >> is expensive for large dictionaries, and makes loops O(N^2). >> >>

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Paddy
On Mar 23, 4:14 pm, Paddy <[EMAIL PROTECTED]> wrote: > On Mar 23, 3:53 pm, John Nagle <[EMAIL PROTECTED]> 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). > >

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Paul Rubin
John Nagle <[EMAIL PROTECTED]> 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: ..."). --

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Paddy
On Mar 23, 3:53 pm, John Nagle <[EMAIL PROTECTED]> 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

Re: Testing for an empty dictionary in Python

2008-03-23 Thread Brian Lane
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 John Nagle 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 if d

Re: Testing for an empty dictionary in Python

2008-03-23 Thread D'Arcy J.M. Cain
On Sun, 23 Mar 2008 08:53:02 -0700 John Nagle <[EMAIL PROTECTED]> 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). Try this: if dict: It should be faster as it

RE: Testing for an empty dictionary in Python

2008-03-23 Thread Ryan Ginstrom
> On Behalf Of John Nagle >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). I believe that the following is fairly efficient: >>> def dict_is_empty(D): for k in D:

Testing for an empty dictionary in Python

2008-03-23 Thread John Nagle
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 -- http://mail.python.org/mailman/listinfo/python-list