Slicing Possible Empty List Python -
i'm finding overlapping elements of 2 lists, if exist, , convert integer.
list_converter = intersection[0]
it returns list 1 or no values. if no value, get:
list_converter = intersection[0] indexerror: list index out of range
is there better way this, or avoid error when no list empty?
you can do:
if intersection: list_converter = intersection[0] else: print "no intersection" # or whatever want if there isn't intersection
in python, empty lists (i.e. []
) evaluate false
, empty list can checked using truth value.
Comments
Post a Comment