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


Groups > comp.lang.python > #95500

Re: Check if dictionary empty with == {}

Date 2015-08-19 18:21 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Check if dictionary empty with == {}
References <af138426-3eb4-4b72-aa0f-45ce7fc5c292@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.19.1440027458.28100.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web