David Ly
David Ly

Reputation: 31606

Is there a cleaner way to chain empty list checks in Python?

I have a fairly complex object (deserialized json, so I don't have too much control over it) that I need to check for the existence of and iterate over a fairly deep elements, so right now I have something like this:

if a.get("key") and a["key"][0] and a["key"][0][0] :
    for b in a["key"][0][0] :
        #Do something

which works, but is pretty ugly. It seems there has to be a better way to do this, so what's a more elegant solution?

Upvotes: 3

Views: 432

Answers (3)

John Millikin
John Millikin

Reputation: 200916

try:
  bs = a["key"][0][0]
# Note: the syntax for catching exceptions is different in old versions
# of Python. Use whichever one of these lines is appropriate to your version.
except KeyError, IndexError, TypeError:   # Python 3
except (KeyError, IndexError, TypeError): # Python 2
  bs = []
for b in bs:

And you can package it up into a function, if you don't mind longer lines:

def maybe_list(f):
  try:
    return f()
  except KeyError, IndexError, TypeError:
    return []

for b in maybe_list(lambda: a["key"][0][0]):

Upvotes: 14

recursive
recursive

Reputation: 86134

I would write a custom indexer function like this:

def safe_indexer(obj, *indices):
    for idx in indices:
        if not obj: break

        if hasattr(obj, "get"):
            obj = obj.get(idx)
        else:
            obj = obj[idx]

    return obj

Usage:

a = {"key": {0: {0: "foo"} } };
print safe_indexer(a, "key", 0, 0)
print safe_indexer(a, "bad", 0, 0)

Output:

foo
None

Upvotes: 3

Peter
Peter

Reputation: 132337

What about this:

try:
    for b in a['key'][0][0]:
        # do stuff.
except KeyError, TypeError, IndexError:
    # respond appropriately.

Upvotes: 2

Related Questions