tf_quant_finance.utils.iterate_nested

Contents

Last updated: 2023-03-16.

tf_quant_finance.utils.iterate_nested#

View source

Creates an iterator over every leaf value in depth first order.

tf_quant_finance.utils.iterate_nested(
    nd, previous_keys=None
)

Iterates over a nested dictionary in depth first order. The order in which the peer keys are traversed is not guaranteed (same as for the keys of a dictionary).

nested_dict = {'a': 1, 'b': [2, 3, 4], 'c': {'d': 8}}
for k, v in iterate_nested(nested_dict):
  print('_'.join(k), v)
# Prints out:
# a: 1
# b: [2, 3, 4]
# c_d: 8

Args:#

  • nd: The dictionary to be traversed.

  • previous_keys: If supplied, the computed key list will be a join of the previous_keys and the current keys.

Yields:#

A tuple of the key path and the value for each leaf node.