nested dictionary comprehension python code example
Example 1: create dictionary comprehension python
{key:value for key in iterable}
Example 2: dict comprehension python
fruits = ["apple", "banana", "cherry"]
print({f: len(f) for f in fruits})
{'apple': 5, 'banana': 6, 'cherry': 6}
Example 3: Nested dictionary Python
IDs = ['emp1','emp2','emp3']
EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
{'name': 'Kim', 'job': 'Dev'},
{'name': 'Sam', 'job': 'Dev'}]
D = dict(zip(IDs, EmpInfo))
print(D)
Example 4: dict comprehension python
keys=['var1','var2','var3']
my_dict={key:np.zeros(10) for key in keys}
Example 5: Nested dictionary Python
D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
'emp2': {'name': 'Kim', 'job': 'Dev'},
'emp3': {'name': 'Sam', 'job': 'Dev'}}