# xarray.DataTree.from_dict

#### *classmethod* DataTree.from_dict(data=None, coords=None, , name=None, nested=False)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/datatree.py#L1271-L1488)

Create a datatree from a dictionary of data objects, organised by paths into the tree.

* **Parameters:**
  * **data** ([dict-like](https://docs.python.org/3/glossary.html#term-mapping), *optional*) – A mapping from path names to `None` (indicating an empty node),
    `DataTree`, `Dataset`, objects coercible into a `DataArray` or
    a nested dictionary of any of the above types.

    Path names should be given as unix-like paths, either absolute
    (/path/to/item) or relative to the root node (path/to/item). If path
    names containing more than one part are given, new tree nodes will
    be constructed automatically as necessary.

    To assign data to the root node of the tree use “”, “.”, “/” or “./”
    as the path.
  * **coords** ([dict-like](https://docs.python.org/3/glossary.html#term-mapping), *optional*) – A mapping from path names to objects coercible into a DataArray, or
    nested dictionaries of coercible objects.
  * **name** (`Hashable | None`, *optional*) – Name for the root node of the tree. Default is None.
  * **nested** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *optional*) – If true, nested dictionaries in `data` and `coords` are
    automatically flattened.
* **Returns:**
  [`DataTree`](xarray.DataTree.html.md#xarray.DataTree)

#### SEE ALSO
[`Dataset`](xarray.Dataset.html.md#xarray.Dataset)

### Notes

`DataTree.from_dict` serves a conceptually different purpose from
`Dataset.from_dict` and `DataArray.from_dict`. It converts a
hierarchy of Xarray objects into a DataTree, rather than converting pure
Python data structures.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('425f8a2b-184d-4b3d-93b1-a860e7d9c191','a753ef4a-10a6-440a-a2d9-63c78f7c897e','efbf8fd8-1923-448e-8e6d-8815f5ff00a7','../lite/tree/../notebooks/index.html?path=fe30cc1e_275b_4d6f_ac21_74c23b0486ce.ipynb','None')">Try it in your browser!</button></div>

Construct a tree from a dict of Dataset objects:

```pycon
>>> dt = DataTree.from_dict(
...     {
...         "/": Dataset(coords={"time": [1, 2, 3]}),
...         "/ocean": Dataset(
...             {
...                 "temperature": ("time", [4, 5, 6]),
...                 "salinity": ("time", [7, 8, 9]),
...             }
...         ),
...         "/atmosphere": Dataset(
...             {
...                 "temperature": ("time", [2, 3, 4]),
...                 "humidity": ("time", [3, 4, 5]),
...             }
...         ),
...     }
... )
>>> dt
<xarray.DataTree>
Group: /
│   Dimensions:  (time: 3)
│   Coordinates:
│     * time     (time) int64 24B 1 2 3
├── Group: /ocean
│       Dimensions:      (time: 3)
│       Data variables:
│           temperature  (time) int64 24B 4 5 6
│           salinity     (time) int64 24B 7 8 9
└── Group: /atmosphere
        Dimensions:      (time: 3)
        Data variables:
            temperature  (time) int64 24B 2 3 4
            humidity     (time) int64 24B 3 4 5
```

Or equivalently, use a dict of values that can be converted into
DataArray objects, with syntax similar to the Dataset constructor:

```pycon
>>> dt2 = DataTree.from_dict(
...     data={
...         "/ocean/temperature": ("time", [4, 5, 6]),
...         "/ocean/salinity": ("time", [7, 8, 9]),
...         "/atmosphere/temperature": ("time", [2, 3, 4]),
...         "/atmosphere/humidity": ("time", [3, 4, 5]),
...     },
...     coords={"/time": [1, 2, 3]},
... )
>>> assert dt.identical(dt2)
```

Nested dictionaries are automatically flattened if `nested=True`:

```pycon
>>> DataTree.from_dict({"a": {"b": {"c": {"x": 1, "y": 2}}}}, nested=True)
<xarray.DataTree>
Group: /
└── Group: /a
    └── Group: /a/b
        └── Group: /a/b/c
                Dimensions:  ()
                Data variables:
                    x        int64 8B 1
                    y        int64 8B 2
```

<div id="efbf8fd8-1923-448e-8e6d-8815f5ff00a7" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('425f8a2b-184d-4b3d-93b1-a860e7d9c191','efbf8fd8-1923-448e-8e6d-8815f5ff00a7')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('425f8a2b-184d-4b3d-93b1-a860e7d9c191','efbf8fd8-1923-448e-8e6d-8815f5ff00a7')">Open In Tab</button></div><div id="a753ef4a-10a6-440a-a2d9-63c78f7c897e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
