# tf.nest.pack_sequence_as

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/nest/pack_sequence_as) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/util/nest.py#L471-L504) |

Returns a given flattened sequence packed into a given structure.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.nest.pack_sequence_as`](https://www.tensorflow.org/api_docs/python/tf/nest/pack_sequence_as)

<br />

    tf.nest.pack_sequence_as(
        structure, flat_sequence, expand_composites=False
    )

If `structure` is a scalar, `flat_sequence` must be a single-element list;
in this case the return value is `flat_sequence[0]`.

If `structure` is or contains a dict instance, the keys will be sorted to
pack the flat sequence in deterministic order. 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 `flatten`.
This correctly repacks dicts and `OrderedDict`s 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.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `structure` | Nested structure, whose structure is given by nested lists, tuples, and dicts. Note: numpy arrays and strings are considered scalars. |
| `flat_sequence` | flat sequence to pack. |
| `expand_composites` | If true, then composite tensors such as [`tf.SparseTensor`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/sparse/SparseTensor) and [`tf.RaggedTensor`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/RaggedTensor) are expanded into their component tensors. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `packed` | `flat_sequence` converted to have the same recursive structure as `structure`. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If `flat_sequence` and `structure` have different element counts. |
| `TypeError` | `structure` is or contains a dict with non-sortable keys. |

<br />