Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (49 sloc) 1.17 KB


Digs into Dicts (or Lists, or Tuples or Complex objects)

Useful syntax for digging into nested dictionaries, lists and tuples, and removes the need to check if a key or index exists, or handling of KeyError or IndexError


$ pip install dict_digger


from dict_digger import dig

h = {
    'a': {
         'b': 'tuna',
         'c': 'fish'
     'b': {}

result = dig(h, 'a','b')
print result  # prints 'tuna'

result = dig(h, 'c','a')
print result # prints None
# Important!!  Does not throw an error, just returns None

# but if you like
result = dig(h, 'c','a', fail=True)
# raises a KeyError

# also supports complex objects so ...

complex = {
    'a': {
     'b': {}
result = dig(complex, 'a',0)
print result #prints tuna


    result = h['c']['a']
except KeyError:
    result = None


We are using nose

$ nosetests
Something went wrong with that request. Please try again.