nehat280
nehat280

Reputation: 51

how do i convert nested dictionary within a dictionary to dictionary

I have a dictionary as below:

nested_dict = { 'dictB': {'dictA': {'key_1': 'value_1'}}}

basically i want the value of key dictB as a dictionary.

i tried doing

  1. dict(nested_dict["dictB"].values()) but i got error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-104-275e88e1ed17> in <module>
----> 1 dict(nested_dict["dictB"].values())

ValueError: dictionary update sequence element #0 has length 1; 2 is required
  1. nested_dict["dictB"].values() . i got dict_values([{'key_1': 'value_1'}]) which is a list itself but i want it to be dictionary is there any way i can get the dictionary of dictB key.

Upvotes: 0

Views: 59

Answers (2)

czyngis
czyngis

Reputation: 421

If you want to get the dictionary in the value assigned to the dictB key, you can access it simply by accessing the value of the key of the nested_dict dictionary as follows: nested_dict['dictB']

Upvotes: 1

Bill the Lizard
Bill the Lizard

Reputation: 405745

Since it's already a dict, you should be able to get it directly without any transformation:

dict_b = nested_dict["dictB"]

Upvotes: 3

Related Questions