how to extract multi level dictionary keys/values in python -
there 2 level dictionary in python:
for instance here: index[term][id] = n
how term
, n
when id = 3
?
or perfect if returns in form result[id] = [term, n]
iterate on nested dict , create new dict map values in desired format. can create custom function like:
def get_tuple_from_value(my_dict): new_dict = {} term, nested_dict in my_dict.items(): id, n in nested_dict.items(): new_dict[id] = [term, n] return new_dict
or, simple dict comprehension like:
{i: [t, n] t, nd in d.items() i, n in nd.items()}
where d
holding dictionary.
Comments
Post a Comment