473,834 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1941
In article <ma************ *************** **********@pyth on.org>,
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.u s (Cameron Laird)
wrote:
In article <ma************ *************** **********@pyth on.org>,
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
11365
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 thing in getThingIterator (): do something.
10
7969
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 items) when some sort of special case handling is required. Typical code for handling an empty list: if somelist: for x in somelist: something(x) else:
4
1928
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 are NO classes in the Classes collection (Classes) of the custom object. The conditional expression returns the error shown below it... =======================================================XML returned by
0
25102
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 steps based on this outcome. There are basically 3 Methods for testing for an Empty Recordset (Recordset that returns no Rows). We will be using DAO, but these Methods are equally applicable to ADO. 'Common Code Block Dim MyDB As DAO.Database, MyRS As...
13
1757
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 (!dataSet.Identity.Chain_ID.Equals(DBNull.Value)) dataRow = dataSet.Identity.Chain_ID; else dataRow = 0;
9
2532
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 into my program and I want to see if any of a set of strings is a substring of the string I have been given. It's quite OK to stop at the first one found. Ideally the strings being searched through will be the keys of a dictionary but this...
24
2534
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 you ensure that the code will work correctly in the future? Short version:
1
214
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
979
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 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.
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9646
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9332
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7760
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6957
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.