This is mostly a question of good/pythonic style. I have a dictionary which has lists for values, i.e.
my_dict = {"a": a_list, "b": b_list, "c": c_list}
and so on. I also have an empty dictionary with the same keys, in which I want to store the mean of these lists against their key. If instead of using a second dictionary, I used a nested list, I could do
mean_lists = [[key, sum(l)/len(l)] for key, l in my_dict.items() if l]
giving an output
[["a", a_mean], ["b", b_mean], ["c", c_mean]]
which seems neat to me. Is there a way to do this nicely outputting as a dictionary, or is something like this:
mean_dict = {key: [] for key in my_dict}
for key, l in my_dict.items():
if l:
mean_dict[key] = sum(l)/len(l)
the best I can do?