Is there a more elegant way to fuse two lists of certain keys in a dictionary into one list in a different dictionary?

Question:

I have a dictionary with lists as values and i want to concatenate lists of certain keys to one and store it in another dictionary.

Right now i always do this:

plot_history = {}
for key in history.keys():
    plot_key = key[3:]
    if plot_key in scores:
        if plot_key in plot_history.keys():
            plot_history[plot_key].extend(history[key])
        else:
            plot_history[plot_key] = history[key]

Example for history:

{'tl_loss': [1.3987799882888794, 1.0936943292617798], 'tl_categorical_accuracy': [0.31684982776641846, 0.3901098966598511], 'tl_val_loss': [1.0042442083358765, 0.7404149174690247], 'tl_val_categorical_accuracy': [0.3589743673801422, 0.5256410241127014], 'ft_loss': [1.6525564193725586, 1.1816378831863403], 'ft_categorical_accuracy': [0.5370370149612427, 0.4157509207725525], 'ft_val_loss': [0.9936744570732117, 1.1621183156967163], 'ft_val_categorical_accuracy': [0.36538460850715637, 0.3910256326198578]}

Example for scores: ["loss", "val_loss"]

And i try to get this:

{'loss': [1.3987799882888794, 1.0936943292617798, 1.6525564193725586, 1.1816378831863403], 'val_loss': [1.0042442083358765, 0.7404149174690247, 0.9936744570732117, 1.1621183156967163]}

But i wonder if there’s a more elegant way.
Thank you for any suggestions.

Asked By: aeffa

||

Answers:

You can use dictionary comprehension:

plot_history = {score: [history[f"tl_{score}"], history[f"ft_{score}"]] for score in scores}
Answered By: RJ Adriaansen

With defaultdict

from collections import defaultdict

plot_history_dd = defaultdict(list)
for key in history.keys():
    plot_key = key[3:]
    if plot_key in scores:
        plot_history_dd[plot_key].extend(history[key])

# Convert to dict for convenience
plot_history_dd = dict(plot_history_dd)
print(plot_history_dd)
Answered By: Tzane
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.