Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95498 > unrolled thread

Check if dictionary empty with == {}

Started byAnton <anschatten@gmail.com>
First post2015-08-19 15:57 -0700
Last post2015-08-20 17:56 +0200
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Check if dictionary empty with == {} Anton <anschatten@gmail.com> - 2015-08-19 15:57 -0700
    Re: Check if dictionary empty with == {} MRAB <python@mrabarnett.plus.com> - 2015-08-20 00:33 +0100
    Re: Check if dictionary empty with == {} Tim Chase <python.list@tim.thechases.com> - 2015-08-19 18:21 -0500
    Re: Check if dictionary empty with == {} Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-08-20 13:16 +1000
      Re: Check if dictionary empty with == {} Steven D'Aprano <steve@pearwood.info> - 2015-08-21 03:44 +1000
    Re: Check if dictionary empty with == {} Laurent Pointal <laurent.pointal@free.fr> - 2015-08-20 17:56 +0200

#95498 — Check if dictionary empty with == {}

FromAnton <anschatten@gmail.com>
Date2015-08-19 15:57 -0700
SubjectCheck if dictionary empty with == {}
Message-ID<af138426-3eb4-4b72-aa0f-45ce7fc5c292@googlegroups.com>
Probably a silly question. 
Let's say I have a dictionary mydict and I need to test if a dictionary is empty.

I would use

if not mydict:
    """do something"""

But I just came across a line of code like:

if mydict == {}:
    """do something"""

which seems odd to me, but maybe there is a valid use case, thus I decided to ask the community.

Thanks.

[toc] | [next] | [standalone]


#95499

FromMRAB <python@mrabarnett.plus.com>
Date2015-08-20 00:33 +0100
Message-ID<mailman.18.1440027240.28100.python-list@python.org>
In reply to#95498
On 2015-08-20 00:15, Skip Montanaro wrote:
> Comparison against {} will be less efficient. You need to create a
> dictionary every time instead of just checking the length of your
> dictionary, which is likely stored in the header. So, it will work, but
> certainly isn't idiomatic Python.
>
Well, that depends on the intention.

Is it checking whether the dictionary is empty, or whether it's an
empty dictionary (and not, say, an empty list)?

[toc] | [prev] | [next] | [standalone]


#95500

FromTim Chase <python.list@tim.thechases.com>
Date2015-08-19 18:21 -0500
Message-ID<mailman.19.1440027458.28100.python-list@python.org>
In reply to#95498
On 2015-08-19 15:57, Anton wrote:
> Probably a silly question. 
> Let's say I have a dictionary mydict and I need to test if a
> dictionary is empty.
> 
> I would use
> 
> if not mydict:
>     """do something"""
> 
> But I just came across a line of code like:
> 
> if mydict == {}:
>     """do something"""
> 
> which seems odd to me, but maybe there is a valid use case, thus I
> decided to ask the community.

The only valid reason is "the person who wrote that line doesn't
speak idiomatic Python", and that it should be changed to "if not
mydict" at your next code checking :-D

-tkc


[toc] | [prev] | [next] | [standalone]


#95503

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-08-20 13:16 +1000
Message-ID<55d546a5$0$1564$c3e8da3$5496439d@news.astraweb.com>
In reply to#95498
On Thursday 20 August 2015 08:57, Anton wrote:

> Probably a silly question.
> Let's say I have a dictionary mydict and I need to test if a dictionary is
> empty.
> 
> I would use
> 
> if not mydict:
>     """do something"""
> 
> But I just came across a line of code like:
> 
> if mydict == {}:
>     """do something"""
> 
> which seems odd to me, but maybe there is a valid use case, thus I decided
> to ask the community.

It's neither good code or bad code. It looks like something a beginner or 
casual Python user (e.g. a sys admin) might have written, but that isn't 
necessarily bad. It's no more wrong than writing `if x == 0` to test for an 
"empty" number (or "nothing", in the numeric sense).

Pros:

+ It's pretty short and explicit.

+ It allows for duck-typing: if some de facto mapping object wants to 
  support the dict API without inheriting from dict, it can.

+ Even a beginner can understand it: "does mydict equal an 
  empty dict?"


Cons:

- It looks a bit Python 1.5-ish. People used to more modern 
  idioms may have an (unjustified, in my opinion) "WTF" moment
  when looking at it.

- It *might* be a bit slow, since it takes time to create an
  empty dict; on the other hand, it also takes time to call
  isinstance(), so if you care about this, I want to see your
  profiling results and benchmarks.

Actually, it's not a bit slow, it's *significantly* faster than an 
isinstance check:

steve@runes:~$ python2.7 -m timeit -s "mydict = {1:2}" \
> "if mydict == {}: pass"
10000000 loops, best of 3: 0.0872 usec per loop
steve@runes:~$ python2.7 -m timeit -s "mydict = {1:2}" \
> "if isinstance(mydict, dict) and not mydict: pass"
1000000 loops, best of 3: 0.257 usec per loop

So maybe it's a micro-optimization?


TL;DR

There's nothing wrong with it. It is ever-so-subtly different from the 
various alternatives, so if you change it, don't be surprised if you break 
something.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#95512

FromSteven D'Aprano <steve@pearwood.info>
Date2015-08-21 03:44 +1000
Message-ID<55d611e1$0$1641$c3e8da3$5496439d@news.astraweb.com>
In reply to#95503
On Thu, 20 Aug 2015 11:54 pm, Skip Montanaro wrote:

> On Wed, Aug 19, 2015 at 10:16 PM, Steven D'Aprano <
> steve+comp.lang.python@pearwood.info> wrote:
> 
>> So maybe it's a micro-optimization?
> 
> 
> Note, however, that the original post compared "mydict == {}" with "not
> mydict". In that case, it's decidedly not an optimization:
> 
> firefly% python2.7 -m timeit -s "mydict = {1:2}" "if mydict == {}: pass"
> 10000000 loops, best of 3: 0.0508 usec per loop
> firefly% python2.7 -m timeit -s "mydict = {1:2}" "if not mydict: pass"
> 10000000 loops, best of 3: 0.0246 usec per loop

I suppose it depends on whether you want to run the "if" block when mydict
is None, 0, [], "", etc.

Testing for "any Falsey value" and "an empty dict" are not the same,
naturally they will perform differently.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#95511

FromLaurent Pointal <laurent.pointal@free.fr>
Date2015-08-20 17:56 +0200
Message-ID<55d5f8c3$0$3039$426a34cc@news.free.fr>
In reply to#95498
Anton wrote:

> Probably a silly question.
> Let's say I have a dictionary mydict and I need to test if a dictionary is
> empty.
> 
> I would use
> 
> if not mydict:
>     """do something"""
> 
> But I just came across a line of code like:
> 
> if mydict == {}:
>     """do something"""
> 
> which seems odd to me, but maybe there is a valid use case, thus I decided
> to ask the community.
> 
> Thanks.

You can also use:

if len(mydict) == 0:
    "do something"

The form 'if not mydict:' is AMA the more pythonic, but it may be a source 
of question for beginners (what is the boolean meaning of a dictionnary…).

A+
Laurent.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web