Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!ecngs!feeder.ecngs.de!border1.nntp.ams.giganews.com!nntp.giganews.com!uio.no!nntp.uib.no!svn.schaathun.net!not-for-mail From: Hans Georg Schaathun Newsgroups: comp.lang.python Subject: Re: checking if a list is empty Date: Wed, 11 May 2011 10:02:42 +0100 Organization: University of Bergen Lines: 34 Message-ID: References: <200e93c2-6b87-4113-9c6f-85815e51ea77@28g2000yqu.googlegroups.com> <4dc4b3c5$0$29991$c3e8da3$5496439d@news.astraweb.com> NNTP-Posting-Host: vannskorpion.bccs.uib.no X-Trace: toralf.uib.no 1305104170 78955 129.177.20.20 (11 May 2011 08:56:10 GMT) X-Complaints-To: abuse@uib.no NNTP-Posting-Date: 11 May 2011 08:56:10 GMT User-Agent: slrn/pre1.0.0-18 (Linux) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5100 On 07 May 2011 02:51:50 GMT, Steven D'Aprano wrote: : On Fri, 06 May 2011 14:57:21 -0700, scattered wrote: : : > is there any problem with : > : > (3) if li == []: : > : > ? : > : > Seems to work when I test it and seems to clearly test what you are : > trying to test. The only problem might be if in some contexts == has the : > semantics of checking for object identity. : : Yes, if li == [] works too. But how do you know li is a list and not some : other sequence type? It says so in the Subject header :-) : The advantage of the "if x" test is that it is independent of the type of : x. Sure, but the question wasn't ... The problem with 'if x' is that it requires a much more detailed understanding of python. li == [] is as explicit as it gets, and leaves no room for doubt. len(li) == 0 is almost as explicit and much more flexible. Just x is as generic as it gets, but depends on python's convolved rules for duck processing and if you aim at legibility it is better avoided. -- :-- Hans Georg