Skip to content Skip to sidebar Skip to footer

Splitting Nested Dictionary

I have a nested dictionary as per below. I am looking to remove the initial Data item. To be left with only the inner dictionary {0: 'information1', 1: 'information2', 2: 'informat

Solution 1:

You are not trying to split, but to retrieve one of the values inside the dictionary:

d = {'Data': {0: 'information1', 1: 'information2', 2: 'information3'}}
inner = d['Data']

inner will now contain {0: 'information1', 1: 'information2', 2: 'information3'}

A bit more explanation:

Looking at d, it contains one key/value pair. The key is 'Data' and the value is {0: 'information1', 1: 'information2', 2: 'information3'}.

Now to get the value from d that is associated with the key 'Data', we use the syntax with [] and use the key:

inner = d['Data']

This will return the value and assign that to inner. You can then access the values within inner in the same way. So inner[1] will be information2.

Post a Comment for "Splitting Nested Dictionary"