Wednesday, November 27, 2024
Google search engine
HomeLanguagesHow to convert a nested OrderedDict to dict?

How to convert a nested OrderedDict to dict?

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 package
from collections import OrderedDict
 
# define OrderedDict
od1 = OrderedDict([('1', 'one'), ('2', 'two')])
print(type(od1))
print(od1)
 
# define nested OrderedDict
od2 = 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 package
from collections import OrderedDict
 
# define OrderedDict
od1 = OrderedDict([('1', 'one'), ('2', 'two')])
 
# convert to dict
od1 = dict(od1)
 
# display dictionary
print(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 package
from collections import OrderedDict
import json
 
# define nested OrderedDict
od2 = OrderedDict([('1', 'one'),
                   ('2', OrderedDict([('-2',
                                       '-ive'),
                                      ('+2',
                                       '+ive')]))])
# convert to dict
od2 = json.loads(json.dumps(od2))
 
# display dictionary
print(type(od2))
print(od2)


Output:

<class 'dict'>
{'1': 'one', '2': {'-2': '-ive', '+2': '+ive'}}

RELATED ARTICLES

Most Popular

Recent Comments