Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get nested dictionary keys

example_dict.get('key1', {}).get('key2')
Comment

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

create dictionary from a nested list

my_dict= 'mc': [[{'id': '1.185662381',
        'marketDefinition': {'bspMarket': False,
         'turnInPlayEnabled': True,
         'persistenceEnabled': True,
         'marketBaseRate': 5.0,
         'eventId': '30729399',
         'eventTypeId': '1',
         'numberOfWinners': 1,
         'bettingType': 'ODDS'}],
    [{'id': '1.185662380',
        'marketDefinition': {'bspMarket': True,
         'turnInPlayEnabled': False,
         'persistenceEnabled': True,
         'marketBaseRate': 5.0,
         'eventId': '30729399',
         'eventTypeId': '1',
         'numberOfWinners': 1,
         'bettingType': 'ODDS'}]]
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

Creating a Nested Dictionary

# welcome to softhunt.net
# Creating a Nested Dictionary
Dictionary = {0: 'Softhunt', 1: '.net',
		2:{'i' : 'By', 'ii' : 'Ranjeet', 'iii' : 'Andani'}}
print(Dictionary)
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 :: combination in python math 
Python :: del en python 
Python :: shape function python 
Python :: reference variable python 
Python :: tri python 
Python :: dictionary lookup python 
Python :: how to find gcd of two numbers in python 
Python :: sort pandas dataframe by specific column 
Python :: python default dictionary 
Python :: how to make an argument optional in python 
Python :: tkinter filedialog 
Python :: csv.dictreader 
Python :: math function in python 
Python :: jquery datepicker disable 
Python :: numpy sign method 
Python :: python check if variable has value 
Python :: how to flatten list of lists in python 
Python :: python os check if file with extension exists 
Python :: python printing 
Python :: camp cretaceous.com 
Python :: negate an int in python 
Python :: TypeError: view must be a callable or a list/tuple in the case of include(). 
Python :: make guessing game by python 
Python :: how to select number by twos in a list python next to each 
Python :: python3 vowels and consonants filter 
Python :: how to use visualize_runtimes 
Python :: bassie en adriaan 
Python :: queryset o que é 
Python :: notebook python static website generator 
Shell :: ubuntu XAMPP Starting Apache...fail 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =