Brace Philosophy
Brace Philosophy

Reputation: 27

python how to create dictionaries in dictionaries from lists

I have a list of file names, experiments = ['f1','f2','f3','f4'], times of day, t = ['am','pm'], and types of data collected, ['temp','humidity'].

From these I want to create dictionaries within dictionaries in the following format:

dict = {'f1': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
        'f2': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
        'f3': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
        'f4': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}}}

What's the best way to do this?

Upvotes: 0

Views: 226

Answers (3)

ben author
ben author

Reputation: 2955

A case for comprehensions if I ever saw.

from copy import deepcopy
datatypes = ['temp','humidity']
times = ['am','pm']
experiments = ['f1','f2','f3','f4']

datatypes_dict = dict((k, []) for k in datatypes)
times_dict = dict((k, deepcopy(datatypes_dict)) for k in times)
experiments_dict = dict((k, deepcopy(times_dict)) for k in experiments)

or the nicer dict comprehension way (python 2.7+)

datatypes_dict = {k: [] for k in datatypes}
times_dict = {k: deepcopy(datatypes_dict) for k in times}
experiments_dict = {k: deepcopy(times_dict) for k in experiments}

you can nest them but it gets mind-blowing pretty quick if things are at all complicated.

In this use case, however, @marshall.ward's answer

{z: {y: {x: [] for x in data_types} for y in t} for z in experiments}

is far better than mine, as you can avoid the deepcopy()ing.

Upvotes: 2

John La Rooy
John La Rooy

Reputation: 304137

Taking some artistic license with the output format

>>> from collections import namedtuple, defaultdict
>>> from itertools import product
>>> experiments = ['f1','f2','f3','f4']
>>> times_of_day = ['am','pm']
>>> data_types = ['temp','humidity']
>>> DataItem = namedtuple('DataItem', data_types)
>>> D=defaultdict(dict)
>>> for ex, tod in product(experiments, times_of_day):
...     D[ex][tod]=DataItem([], [])
... 
>>> D
defaultdict(<type 'dict'>, {'f1': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f2': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f3': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f4': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}})

You can access the data items like this

>>> D['f1']['am'].temp
[]
>>> D['f1']['am'].humidity
[]

Upvotes: 1

marshall.ward
marshall.ward

Reputation: 7198

{z: {y: {x: [] for x in data_types} for y in t} for z in experiments}

Upvotes: 5

Related Questions