Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Testing for an empty list

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


alexnbryan at gmail

Jul 3, 2008, 1:58 PM

Post #1 of 4 (169 views)
Permalink
Testing for an empty list

Okay this is a simple question I just don't know how. If I have a list, say:

funList = []

and after a while something possible should have been appended to it, but
wasn't. How can I test if that list is empty.
--
View this message in context: http://www.nabble.com/Testing-for-an-empty-list-tp18268092p18268092.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list


elessar at nienna

Jul 3, 2008, 2:05 PM

Post #2 of 4 (155 views)
Permalink
Re: Testing for an empty list [In reply to]

Alexnb wrote:
> Okay this is a simple question I just don't know how. If I have a list, say:
>
> funList = []
>
> and after a while something possible should have been appended to it, but
> wasn't. How can I test if that list is empty.

if not funList:
do_something()

-Matt
--
http://mail.python.org/mailman/listinfo/python-list


claird at lairds

Jul 3, 2008, 7:02 PM

Post #3 of 4 (150 views)
Permalink
Re: Testing for an empty list [In reply to]

In article <mailman.37.1215119171.20628.python-list [at] python>,
Matthew Fitzgibbons <elessar [at] nienna> wrote:
>Alexnb wrote:
>> Okay this is a simple question I just don't know how. If I have a list, say:
>>
>> funList = []
>>
>> and after a while something possible should have been appended to it, but
>> wasn't. How can I test if that list is empty.
>
>if not funList:
> do_something()
.
.
.
It's also perfectly legitimate--and arguably even more
precise--to write

if funList == []:
do_something()
--
http://mail.python.org/mailman/listinfo/python-list


roy at panix

Jul 3, 2008, 7:26 PM

Post #4 of 4 (153 views)
Permalink
Re: Testing for an empty list [In reply to]

In article <luc0k5-spf.ln1 [at] lairds>, claird [at] lairds (Cameron Laird)
wrote:

> In article <mailman.37.1215119171.20628.python-list [at] python>,
> Matthew Fitzgibbons <elessar [at] nienna> wrote:
> >Alexnb wrote:
> >> Okay this is a simple question I just don't know how. If I have a list,
> >> say:
> >>
> >> funList = []
> >>
> >> and after a while something possible should have been appended to it, but
> >> wasn't. How can I test if that list is empty.
> >
> >if not funList:
> > do_something()
> .
> .
> .
> It's also perfectly legitimate--and arguably even more
> precise--to write
>
> if funList == []:
> do_something()

Any of these will be true for an empty list and false for a non-empty list:

not funList
len(funList) == 0
funList == []

Where they differ is how they behave for values of funList which are not
lists. For example, if you did funList = (), then the first two would be
true and the last one false. If you did funList = 0, the first and last
would be true, and the middle one would raise an exception.

The point is that if you're *sure* the item in question is going to be a
list, then any of them are pretty much as good as any other. If it's a
parameter that's being passed into a routine, so you can't be sure what
type it is, then you should be thinking a little harder about how flexible
you want to be.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.