# How to return an arbitrary number of items from a calcfunction/workfunction

**URL:** <https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 3, 2024, 12:39am UTC](https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377 "2024-05-03T00:39:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cote3804](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/cote3804/32/99_2.png) [@cote3804](https://aiida.discourse.group/u/cote3804)\
**Post date:** [May 3, 2024, 12:39am UTC](https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377/1 "2024-05-03T00:39:28Z")

</div>

Hello,

Let’s say I want to use AiiDA to wrap some python functions I have that generate many molecule configurations so that I can compare them and pipe them into other workflows. As my functions are currently implemented, the number of molecules returned is variable.

```auto
def build_configuration(atoms) -> Atoms:
    atoms.append(random_atom) 
    return atoms

@calcfunction
def generate_configurations(atoms, num_configs):
    configurations = List()
    for config in num_configs:
        config_atoms = build_configuration(atoms)
        config_struct = StructureData(ase=config_atoms)
        configurations.append(config_struct)
    return configurations

```

I want all of these configurations stored in provenance, but I don’t care how that’s done. One method I’ve tried is to turn all of these into StructureData objects, and pack them all into an aiida List that the calcfunction returns (shown above in pseudocode). I suspect part of why this doesn’t work is because I’d need to return the StructureData nodes individually from calc functions and then pack them into a List.

Do I have to pack all of this into a WorkChain? It isn’t clear to me how to do that given that I still need to define (I think) a set number of outputs in the spec.

I’ve looked around the documentation to find an example where multiple things are being returned, but couldn’t find anything.

Thanks AiiDA team!

---

<div class="post-metadata">

**Author:** ![t-reents](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/t-reents/32/82_2.png) [@t-reents](https://aiida.discourse.group/u/t-reents)\
**Post date:** [May 3, 2024, 5:58am UTC](https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377/2 "2024-05-03T05:58:57Z")

</div>

Hi @cote3804

you can use a `dict` to return multiple results from a `calcfunction` or `workfunction`. Taking your example above, this could look like the following:

```auto
@calcfunction
def generate_configurations(atoms, num_configs):
    configurations = []
    for config in num_configs:
        config_atoms = build_configuration(atoms)
        config_struct = StructureData(ase=config_atoms)
        configurations.append(config_struct)
    return {f'configuration{i}': config for i, config in enumerate(configurations)}

```

If you don’t care about a specific label, you can of course simply iterate the `dict.values()` afterwards.

---

<div class="post-metadata">

**Author:** ![Xing](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/xing/32/10_2.png) [@Xing](https://aiida.discourse.group/u/Xing)\
**Post date:** [May 3, 2024, 6:03am UTC](https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377/3 "2024-05-03T06:03:50Z")

</div>

Please check this related [issue](https://github.com/aiidateam/aiida-core/issues/6354) by @mbercx .

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/aiida/original/1X/c417197d3750949351575d03ed03f5e2ad32e649.png) [@system](https://aiida.discourse.group/u/system)\
**Post date:** [May 3, 2026, 6:03am UTC](https://aiida.discourse.group/t/how-to-return-an-arbitrary-number-of-items-from-a-calcfunction-workfunction/377/4 "2026-05-03T06:03:59Z")

</div>

This topic was automatically closed 730 days after the last reply. New replies are no longer allowed.
