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


Groups > comp.lang.python > #95500

Re: Check if dictionary empty with == {}

Path csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; '"if': 0.09; 'python",': 0.09; 'question.': 0.13; '-tkc': 0.16; 'empty.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'silly': 0.16; 'wrote:': 0.16; 'odd': 0.18; 'seems': 0.23; 'wrote': 0.23; 'thus': 0.24; 'header :In-Reply-To:1': 0.24; "doesn't": 0.26; 'checking': 0.27; 'community.': 0.27; 'dictionary': 0.29; 'code': 0.30; 'probably': 0.31; '"the': 0.32; 'maybe': 0.33; 'changed': 0.33; 'case,': 0.34; 'next': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'say': 0.37; 'charset:us-ascii': 0.37; 'speak': 0.38; 'test': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'your': 0.60; 'received:46': 0.63; 'decided': 0.66; 'idiomatic': 0.84; 'subject:Check': 0.95
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1440026492595:3849385184
X-MC-Ingress-Time 1440026492595
Date Wed, 19 Aug 2015 18:21:13 -0500
From Tim Chase <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: Check if dictionary empty with == {}
In-Reply-To <af138426-3eb4-4b72-aa0f-45ce7fc5c292@googlegroups.com>
References <af138426-3eb4-4b72-aa0f-45ce7fc5c292@googlegroups.com>
X-Mailer Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu)
MIME-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.19.1440027458.28100.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440027458 news.xs4all.nl 23760 [2001:888:2000:d::a6]:33094
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 3480
X-Received-Body-CRC 605267520
Xref csiph.com comp.lang.python:95500

Show key headers only | 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