You can use not operator to check if a list is empty in Python. The same method can be used to check if any datatype is empty in Python as shown in the below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
def checkEmpty(b): if not b: print("is empty") else: print("not empty") a = [] checkEmpty(a) ## is empty a = [1,2,3,4] checkEmpty(a) ## not empty a = () checkEmpty(a) ## is empty a = (1,2,3,4) checkEmpty(a) ## not empty a = {} checkEmpty(a) ## is empty a = {"a": 1,"b": 2} checkEmpty(a) ## not empty a = '' checkEmpty(a) ## is empty a = 'something' checkEmpty(a) ## not empty a = 5 checkEmpty(a) ## not empty |