How to check if a list exists in Python

What is the easiest way to check if a list or dict exists in python?

Im using the following, but this does not work:

if len(list) == 0: print "Im not here" 

Thanks,

+4
source share
7 answers

For lists:

 if a_list: print "I'm not here" 

Same thing for dicts:

 if a_dict: print "I'm not here" 
+4
source

You can use the try / except block:

 try: #work with list except NameError: print "list isn't defined" 
+7
source

When trying to refer to a nonexistent variable, the interpreter raises a NameError . However, it is unsafe to rely on the existence of a variable in your code (it is better to initialize it to None or something else). Sometimes I used this:

 try: mylist print "I'm here" except NameError: print "I'm not here" 
+5
source

If you can name it - it obviously "exists" - I assume that you want to check that it is "not empty" ... The most pythonic method is to use if varname: Note that this will not work with generators / iterators to check if they will return data since the result will always be True .

If you just want to use a specific index / key, just try and use it:

 try: print someobj[5] except (KeyError, IndexError) as e: # For dict, list|tuple print 'could not get it' 
+3
source

Examples:

 mylist=[1,2,3] 'mylist' in locals().keys() 

Or use this:

 mylist in locals().values() 
+1
source

Simple console test:

 >>> if len(b) == 0: print "Ups!" ... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'b' is not defined 

 >>> try: ... len(b) ... except Exception as e: ... print e ... name 'b' is not defined 

Examples show how to check if a list contains items:

 alist = [1,2,3] if alist: print "I'm here!" Output: I'm here! 

Otherwise:

 alist = [] if not alist: print "Somebody here?" Output: Somebody here? 

If you need to check for the existence / non-existence of a list / tuple, maybe this can help:

 from types import ListType, TupleType a_list = [1,2,3,4] a_tuple = (1,2,3,4) # for an existing/nonexisting list # "a_list" in globals() check if "a_list" is defined (not undefined :p) if "a_list" in globals() and type(a_list) is ListType: print "I'm a list, therefore I am an existing list! :)" # for an existing/nonexisting tuple if "a_tuple" in globals() and type(a_tuple) is TupleType: print "I'm a tuple, therefore I am an existing tuple! :)" 

If we need to avoid in globals () , we can use this:

 from types import ListType, TupleType try: # for an existing/nonexisting list if type(ima_list) is ListType: print "I'm a list, therefore I am an existing list! :)" # for an existing/nonexisting tuple if type(ima_tuple) is TupleType: print "I'm a tuple, therefore I am an existing tuple! :)" except Exception, e: print "%s" % e Output: name 'ima_list' is not defined --- name 'ima_tuple' is not defined 

Bibliography: 8.15. types - names for built-in types - Python v2.7.3 documentation http://goo.gl/82TSr

0
source

Check (1) the exist variable and (2) check that it is a list

 try: if type(myList) is list: print "myList is list" else: print "myList is not a list" except: print "myList not exist" 
0
source

All Articles