iNoob
iNoob

Reputation: 1395

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: 'information3'}.

All the information I have found so far only suggests splitting based on value and as I am after the value of Data I am not entirely sure how to specify the split.

Initial Nested Dictionary

{'Data': {0: 'information1', 1: 'information2', 2: 'information3'}}

Expected Result Dictionary

{0: 'information1', 1: 'information2', 2: 'information3'}

Upvotes: 1

Views: 5983

Answers (1)

rje
rje

Reputation: 6418

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.

Upvotes: 4

Related Questions