How to convert a nested OrderedDict to dict? - GeeksforGeeks

23 Nov, 2021
In this article, we will discuss how to convert a nested OrderedDict to dict? Before this we must go through some concepts:
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds the key:value pair. Key-value is provided in the dictionary to make it more optimized.
An OrderedDict is a dictionary subclass that remembers the order in that keys were first inserted.
The only difference between dict() and OrderedDict() is that: OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order and iterating it gives the values in an arbitrary order. By contrast, the order in the items are inserted is remembered by OrderedDict.
To define the nested OrderedDict, we are using the collections module in python.
Python3
#import packagefrom collections import OrderedDict # define OrderedDictod1 = OrderedDict([('1', 'one'), ('2', 'two')])print(type(od1))print(od1) # define nested OrderedDictod2 = OrderedDict([('1', 'one'),                   ('2', OrderedDict([('-2',                                       '-ive'),                                      ('+2',                                       '+ive')]))])print(type(od2))print(od2)
Output:
<class ‘collections.OrderedDict’>
OrderedDict([(‘1’, ‘one’), (‘2’, ‘two’)])
<class ‘collections.OrderedDict’>
OrderedDict([(‘1’, ‘one’), (‘2’, OrderedDict([(‘-2’, ‘-ive’), (‘+2’, ‘+ive’)]))])
We can convert the OrderedDict to dict in just one line, but this is for only simple OrderedDict, not for the nested OrderedDict.
Python3
# import packagefrom collections import OrderedDict # define OrderedDictod1 = OrderedDict([('1', 'one'), ('2', 'two')]) # convert to dictod1 = dict(od1) # display dictionaryprint(type(od1))print(od1)
Output:
<class 'dict'>
{'1': 'one', '2': 'two'}
So, to convert nested OrderedDict to dict, we are using json.loads() and json.dumps() methods. 
The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within { }. It is similar to the dictionary in Python.
The json.loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.
The json.dumps() function converts a Python object into a json string.
Python3
# import packagefrom collections import OrderedDictimport json # define nested OrderedDictod2 = OrderedDict([('1', 'one'),                   ('2', OrderedDict([('-2',                                       '-ive'),                                      ('+2',                                       '+ive')]))])# convert to dictod2 = json.loads(json.dumps(od2)) # display deictionaryprint(type(od2))print(od2)
Output:
<class 'dict'>
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}
ddeevviissaavviittaa
Picked
Python collections-module
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Python | os.path.join() method
Defaultdict in Python
Selecting rows in pandas DataFrame based on conditions
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()