[Tutor] Empty list validation

simone simozack at yahoo.it
Mon Jun 9 17:51:23 CEST 2008


qsqgeekyogdty at tiscali.co.uk 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

So, consider this example:

 >>> def isEmpty(l):
	if l:
		print "The list is NOT empty!"
	else:
		print "The list is empty!"

		
 >>> list = []
 >>> isEmpty(list)
The list is empty!
 >>> list = ['hi']
 >>> isEmpty(list)
The list is NOT empty!
 >>>

HTH,
Simone
Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the Tutor mailing list