DEV Community

Discussion on: Are there functions similar to Ruby's `dig` in other languages?

Collapse
 
ajkerrigan profile image
AJ Kerrigan • Edited

For Python, glom is a great package for this sort of nested data access. The happy path from the intro docs looks like this:

from glom import glom

target = {'a': {'b': {'c': 'd'}}}
glom(target, 'a.b.c')  # returns 'd'
Enter fullscreen mode Exit fullscreen mode

That 'a.b.c' bit is a simple glom specifier, but it can get much wilder! Glom provides more advanced specifiers for features like:

  • Null coalescing behavior
  • Validation
  • Debugging/inspection

And you can always plug in your own code or extend glom even further. Simple to get started, but a long fun road to travel from there!