Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
#         'emp2': {'name': 'Kim', 'job': 'Dev'},
#         'emp3': {'name': 'Sam', 'job': 'Dev'}}
Comment

Accessing elements from a Python Nested Dictionary

# welcome to softhunt.net
# Creating a Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
		2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}}
print('Dictionary', Dictionary)

# Accessing element using key
print(Dictionary[0])
print(Dictionary[2]['i'])
print(Dictionary[2]['ii'])
Comment

python nested object to dict

def my_dict(obj):
    if not  hasattr(obj,"__dict__"):
        return obj
    result = {}
    for key, val in obj.__dict__.items():
        if key.startswith("_"):
            continue
        element = []
        if isinstance(val, list):
            for item in val:
                element.append(my_dict(item))
        else:
            element = my_dict(val)
        result[key] = element
    return result
Comment

Nested dictionary Python

D = {'emp1': {'name': 'Bob', 'job': 'Mgr'},
     'emp2': {'name': 'Kim', 'job': 'Dev'},
     'emp3': {'name': 'Sam', 'job': 'Dev'}}
Comment

nested dict

def dict_generator(indict, pre=None):
    pre = pre[:] if pre else []
    if isinstance(indict, dict):
        for key, value in indict.items():
            if isinstance(value, dict):
                for d in dict_generator(value, pre + [key]):
                    yield d
            elif isinstance(value, list) or isinstance(value, tuple):
                for v in value:
                    for d in dict_generator(v, pre + [key]):
                        yield d
            else:
                yield pre + [key, value]
    else:
        yield pre + [indict]
Comment

python create nested dictionary

people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
          2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}}

print(people)
Comment

make nested dict from two dict

data = [
    [14, 77766, [2, 2]],
    [15, 77766, [1, 2]],
    [70, 88866, [1, 5]],
    [71, 88866, [2, 5]],
    [72, 88866, [5, 5]],
    [73, 88866, [4, 5]],
    [74, 88866, [3, 5]],
    [79, 99966, [1, 2]],
    [80, 99966, [2, 2]],
]

c = {}
for key, id_, (value, _) in data:
    c.setdefault(id_, {})[key] = value
print(c)
Comment

PREVIOUS NEXT
Code Example
Python :: Python NumPy expand_dims Function Syntax 
Python :: python array of objects 
Python :: string pythhon 
Python :: how to use str() 
Python :: import a module in python 
Python :: turn numpy function into tensorflow 
Python :: adding array to array python 
Python :: Install Python2 and Python 3 
Python :: python string: .title() 
Python :: oops python 
Python :: random.random 
Python :: python string 
Python :: nested dictionary python 
Python :: python add 
Python :: function definition python 
Python :: Tree recursive function 
Python :: python how to loop 
Python :: subtract constant from list 
Python :: buble short 
Python :: Search for a symmetrical inner elements of a list python 
Python :: Print only small Latin letters a found in the given string. 
Python :: what will be the output of the following python code? i = 0 while i < 5: print(i) i += 1 if i == 3: break else: print(0) 
Python :: b-spline quantile regression with statsmodels 
Python :: receive ouput subprocess call 
Python :: check package without importing it python 
Python :: PyQt5 change keyboard/tab behaviour in a table 
Python :: df.iterrows write to column 
Python :: What is the expected value of print to this program X = 2 Y = 3 Z = X + Y print(Y) #Z 
Python :: auto clipping path when upload image using python 
Python :: 10.4.1.3. return Terminates Function Execution 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =