Thank you all for the replies.
the 'else' is this necessary, surely when you have an:
>>> if x:
... print 'x'
...print 'y'
is this correct?
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
[EMAIL PROTECTED] ha scritto:
Sorry if this is a basic question, but how do I check if list is empty
or not and return True or False ;)
Normally, in Python, there are these evaluation:
1) True == 1
2) False == 0
3) Empty or have not value == False == 0
4) Not empty or have value == True == 1
On Mon, Jun 9, 2008 at 10:25 AM, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> Hello
> Sorry if this is a basic question, but how do I check if list is empty
> or not and return True or False ;)
You can test the list directly, if it is empty it will evaluate to False:
if (myList):
print 'Somet
One another simple method is to check its count using the *len *method.
eg:
if len(mylist) == 0:
print "empty list"
This is particularly useful in situations where checking emptiness alone is
not enough.
On Mon, Jun 9, 2008 at 8:24 PM, W W <[EMAIL PROTECTED]> wrote:
> >>> def empty_or_not
>>> def empty_or_not(mylist):
... if mylist:
... print "Not empty"
... elif not mylist:
... print "Empty!"
...
>>> empty_or_not([])
Empty!
>>> foo = []
>>> empty_or_not(foo)
Empty!
>>> foo.append('hi')
>>> empty_or_not(foo)
Not empty
My guess is that if any object is empty,
Hello
Sorry if this is a basic question, but how do I check if list is empty
or not and return True or False ;)
Thanks
David
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor