python 3.x - Finding and returning the value at location index in a list -
could current code work function? here goal of trying first , current attempt
look @ list of values, xs. find , return value @ location index. when index invalid xs, return response instead.
current code:
def get(xs, index, response=none): xs=[] i=0 in xs: try: while i<len(xs): index=xs.index(xs,index) index = return except: return response thanks appreciated
you seem confused trying do...
so if understand correctly want return xs[index] if exists otherwise response, simple way following eafp principle is:
def get(xs, index, response=none): try: return xs[index] except indexerror: return response
Comments
Post a Comment