I have 2 CSV files: 'Data' and 'Mapping':
I know how to use dict when only 2 columns are present (1 is needed to be mapped) but I don't know how to accomplish this when 3 columns need to be mapped. Following is the code using which I tried to accomplish mapping of
It returns After some researching, I think I need to create a nested dict, but I don't have any idea how to do this.
smci 30.5k18 gold badges110 silver badges145 bronze badges asked May 2, 2013 at 8:14
4 A nested dict is a dictionary within a dictionary. A very simple thing.
You can also use a
You can populate that however you want. I would recommend in your code something like the following:
According to your comment:
My suggestion would be something like this (without using defaultdict):
Please note though, that for parsing csv files there is a csv module.
answered May 2, 2013 at 8:24
Inbar RoseInbar Rose 39.8k24 gold badges82 silver badges128 bronze badges 1 UPDATE: For an arbitrary length of a nested dictionary, go to this answer. Use the defaultdict function from the collections. High performance: "if key not in dict" is very expensive when the data set is large. Low maintenance: make the code more readable and can be easily extended.
answered Dec 7, 2015 at 20:22
JunchenJunchen 1,7192 gold badges18 silver badges25 bronze badges 3 For arbitrary levels of nestedness:
answered Mar 30, 2016 at 4:18
andrewandrew 1,78319 silver badges19 bronze badges 1 It is important to remember when using defaultdict and similar nested dict modules such as Here is a Python3 example with
Output is:
smci 30.5k18 gold badges110 silver badges145 bronze badges answered Mar 3, 2017 at 20:26
Gerard GGerard G 1512 silver badges4 bronze badges
References:
answered Apr 20, 2021 at 10:06
XerCisXerCis 6295 silver badges4 bronze badges If you want to create a nested dictionary given a list (arbitrary length) for a path and perform a function on an item that may exist at the end of the path, this handy little recursive function is quite helpful:
Example:
answered Oct 7, 2020 at 12:10
conmakconmak 9928 silver badges13 bronze badges
this means to create four empty dict inside data_dict
The output {'dict0': {'val': 'a', 'val1': 'a1', 'val2': 'a2', 'val3': 'a3'}, 'dict1': {'val': 'b', 'val1': 'b1', 'val2': 'b2', 'val3': 'b3'},'dict2': {'val': 'c', 'val1': 'c1', 'val2': 'c2', 'val3': 'c3'}, 'dict3': {'val': 'd', 'val1': 'd1', 'val2': 'd2', 'val3': 'd3'}} answered Jan 11, 2021 at 8:38
Shah VipulShah Vipul 4484 silver badges9 bronze badges
'd' can be used now to store the nested key value pairs. answered Mar 10, 2021 at 14:44
4b0 20.8k30 gold badges92 silver badges137 bronze badges answered Mar 26, 2021 at 8:47
1
You can initialize an empty
ndicts is on Pypi
answered Mar 8 at 17:12
edd313edd313 6814 silver badges14 bronze badges Can we merge two dictionaries in Python?In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.
How do you make nested dictionary?To create a nested dictionary, simply pass dictionary key:value pair as keyword arguments to dict() Constructor. You can use dict() function along with the zip() function, to combine separate lists of keys and values obtained dynamically at runtime.
|