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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -