ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more


Returns a flat list from a given nested structure.

Used in the notebooks

Used in the guide Used in the tutorials

If nest is not a structure , tuple (or a namedtuple), dict, or an attrs class, then returns a single-element list: [nest].

This is the inverse of the nest.pack_sequence_as method that takes in a flattened list and re-packs it into the nested structure.

In the case of dict instances, the sequence consists of the values, sorted by key to ensure deterministic behavior. This is true also for OrderedDict instances: their sequence order is ignored, the sorting order of keys is used instead. The same convention is followed in nest.pack_sequence_as. This correctly repacks dicts and OrderedDicts after they have been flattened, and also allows flattening an OrderedDict and then repacking it back using a corresponding plain dict, or vice-versa. Dictionaries with non-sortable keys cannot be flattened.

Users must not modify any collections used in nest while this function is running.


  1. Python dict (ordered by key):
  dict = { "key3": "value3", "key1": "value1", "key2": "value2" }
    ['value1', 'value2', 'value3']
  1. For a nested python tuple:
  tuple = ((1.0, 2.0), (3.0, 4.0, 5.0), 6.0)
        [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
  1. For a nested dictionary of dictionaries:
  dict = { "key3": {"c": (1.0, 2.0), "a": (3.0)},
  "key1": {"m": "val1", "g": "val2"} }
    ['val2', 'val1', 3.0, 1.0, 2.0]
  1. Numpy array (will not flatten):
  array = np.array([[1, 2], [3, 4]])
        [array([[1, 2],
                [3, 4]])]
  1. tf.Tensor (will not flatten):
  tensor = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
        [<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
          array([[1., 2., 3.],
                 [4., 5., 6.],
                 [7., 8., 9.]], dtype=float32)>]
  1. tf.RaggedTensor: This is a composite tensor thats representation consists of a flattened list of 'values' and a list of 'row_splits' which indicate how to chop up the flattened list into different