473,324 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Re: Testing for an empty list

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
Jul 3 '08 #1
2 1919
In article <ma*************************************@python.or g>,
Matthew Fitzgibbons <el*****@nienna.orgwrote:
>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()
Jul 4 '08 #2
In article <lu************@lairds.us>, cl****@lairds.us (Cameron Laird)
wrote:
In article <ma*************************************@python.or g>,
Matthew Fitzgibbons <el*****@nienna.orgwrote:
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.
Jul 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Roy Smith | last post by:
In the old days, if I wanted to return a sequence of items, I'd return a list, and loop over it like this: for thing in getListOfThings (): do something With iterators, I'm doing: for...
10
by: Brian Roberts | last post by:
I'm using using generators and iterators more and more intead of passing lists around, and prefer them. However, I'm not clear on the best way to detect an empty generator (one that will return no...
4
by: a | last post by:
I'm having trouble testing a custom object. I've tried many different approaches. One is shown below. The XML below shows the state of the object and I'm trying to test for that state, ie there...
0
ADezii
by: ADezii | last post by:
When you create a Recordset, you may want to know immediately whether that Recordset actually contains any Rows. There are Recordsets that don't return any Rows and you may need to take different...
13
by: Fred Chateau | last post by:
I can't seem to find a test for DBNulls. Whatever I try doesn't work. for (int x = 0; x < dataSet.Identity.Rows.Count; x++) { DataRow dataRow = dataSet.Tables.NewRow(); if...
9
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
1
by: c0mrade | last post by:
Try something like this... list = for n, it in enumerate(list): if not it: print 'Error on this definition' else: print '%d. %s' % (n+1, it) Results: 1. lkdfjsldk Error on this definition
0
by: Alexnb | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.