from typing import OrderedDict


def join_dicts(d, keys=None):
    """
    Task: Concatenate list/directory of dictionaries: [d1, d2, ... ] or {"d1": d1, "d2": d2, ...}
          into a single dictionary.

          Note: This function returns a ordered dictionary, i.e, the order of the key insertions
                is preserved when for example .keys()-function is used.

    :param d: List or directory of directories
    :param keys: Range for lists
                 Keys for dictionaries
    :return: Single ordered dictionary containing all the (key, value)-pairs
             from the separate dictionaries.

    :example:
        {"s1": {("mol1","s1"): [...], ("mol2","s1"): [...], ...},
         "s2": {("mol1","s2"): [...], ("mol3","s2"): [...], ...}}

        -->

        {("mol1","s1"): [...], ("mol2","s1"): [...], ..., ("mol1","s2"): [...], ("mol3","s2"): [...], ...}
    """
    if keys is None:
        keys = d.keys()

    d_out = OrderedDict()
    for key in keys:
        d_out.update(d[key])

    return d_out