Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Testing for an empty dictionary in Python

Reply
Thread Tools

Testing for an empty dictionary in Python

 
 
Ryan Ginstrom
Guest
Posts: n/a
 
      03-23-2008
> 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:
return False
return True

>>> dict_is_empty(dict(a=1))

False
>>> dict_is_empty({})

True

Regards,
Ryan Ginstrom

 
Reply With Quote
 
 
 
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      03-23-2008
On Sun, 23 Mar 2008 08:53:02 -0700
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).


Try this:

if dict:

It should be faster as it only checks whether or not there are
any members and does not run keys() or len() on the dictionary. Of
course, you should test it.

--
D'Arcy J.M. Cain <> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
 
John Nagle
Guest
Posts: n/a
 
      03-23-2008
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
 
Reply With Quote
 
Brian Lane
Guest
Posts: n/a
 
      03-23-2008
-----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 dict:
...



- --
- ---[Office 68.7F]--[Outside 42.9F]--[Server 100.4F]--[Coaster 68.0F]---
- ---[ KLAHOWYA WSF (366773110) @ 47 31.2076 -122 27.2249 ]---
Software, Linux, Microcontrollers http://www.brianlane.com
AIS Parser SDK http://www.aisparser.com
Movie Landmarks Search Engine http://www.movielandmarks.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFH5nz/Iftj/pcSws0RAnnMAJoDX9P0cK+RshuvuRRfkyJ4CPwqxACeMWkF
pq7AKr/qzVWyVat0QiTtUfo=
=bpei
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      03-23-2008
On Mar 23, 3:53 pm, John Nagle <na...@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.
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-23-2008
John Nagle <> 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: ...").
 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      03-23-2008
On Mar 23, 4:14 pm, Paddy <paddy3...@googlemail.com> wrote:
> On Mar 23, 3:53 pm, John Nagle <na...@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.
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      03-23-2008
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).
>>
>> John Nagle

>
> if dict:


Cute.

I'd already figured out that

len(dict)

works, which is probably better than

len(dict.keys() > 0

which requires creating a list.

John Nagle
 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      03-23-2008
On Mar 23, 5:45*pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> John Nagle <na...@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

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-23-2008
On Mar 24, 2:53 am, John Nagle <na...@animats.com> 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 dictionaries, and makes loops O(N^2).


I don't understand "makes loops O(N^2)" ... what I see in the
dict_keys function in Objects/dictobject.c is that it makes one linear
pass through its table, ignoring unused and formerly-used slots; seems
like O(N) where N is the size of the table. Where did you get O(N^2)
from?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
empty set and empty dict for Python 3 Yingjie Lan Python 1 07-15-2010 12:49 PM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57