What is the easiest way to check to see if a list or dict exists in python ?
Im using the following but this isn't working:
if len(list) == 0:
print "Im not here"
Thanks,
What is the easiest way to check to see if a list or dict exists in python ?
Im using the following but this isn't working:
if len(list) == 0:
print "Im not here"
Thanks,
For the lists:
if a_list:
print "I'm not here"
The same is for the dicts:
if a_dict:
print "I'm not here"
You can use a try/except block:
try:
#work with list
except NameError:
print "list isn't defined"
When you try to reference a non-existing variable the interpreter raises NameError
. It's not safe, however, to rely on the existence of a variable in your code (you'd better initialize it to None or something). Sometimes I used this:
try:
mylist
print "I'm here"
except NameError:
print "I'm not here"
If you're able to name it - it obviously "exists" - I assume you mean to check that it's "non-empty"... The most pythonic method is to use if varname:
. Note, this won't work on generators/iterables to check if they will return data as the result will always be True
.
If you just want to use a certain index/key, then just try and use it:
try:
print someobj[5]
except (KeyError, IndexError) as e: # For dict, list|tuple
print 'could not get it'
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 has elements:
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 existence/nonexistence of a list/tuple maybe this can be of 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 of in globals() maybe 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 https://docs.python.org/3/library/types.html
Check (1) variable exist and (2) check it is list
try:
if type(myList) is list:
print "myList is list"
else:
print "myList is not a list"
except:
print "myList not exist"
s = set([1, 2, 3, 4])
if 3 in s:
print("The number 3 is in the list.")
else:
print("The number 3 is NOT in the list.")
You can find more about it here: https://docs.quantifiedcode.com/python-anti-patterns/performance/using_key_in_list_to_check_if_key_is_contained_in_a_list.html