Last updated: Apr 10, 2024
Reading timeยท5 min

Use the dict() class to convert an OrderedDict to a regular dict, e.g.
dictionary = dict(ordered_dict).
The dict() class can be passed an iterable of key-value pairs and returns a
new dictionary.
from collections import OrderedDict import json # โ Convert an OrderedDict to a regular Dict ordered_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) dictionary = dict(ordered_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz', 'age': 30} print(dictionary) print(type(dictionary)) # ๐๏ธ <class 'dict'> # --------------------------------------------------- # โ Convert a nested OrderedDict to a regular dict ordered_dict = OrderedDict( [('name', 'bobbyhadz'), ('address', OrderedDict([('post_code', 123)])), ] ) json_str = json.dumps(ordered_dict) regular_dict = json.loads(json_str) # ๐๏ธ {'name': 'bobbyhadz', 'address': {'post_code': 123}} print(regular_dict) print(type(regular_dict)) # ๐๏ธ <class 'dict'>

The first example uses the dict() class to convert an OrderedDict object to
a dict.
OrderedDict to the dict() class if it isn't a nested OrderedDict object.from collections import OrderedDict ordered_dict = OrderedDict( [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] ) dictionary = dict(ordered_dict) # ๐๏ธ {'first': 'bobby', 'last': 'hadz', 'age': 30} print(dictionary) print(type(dictionary)) # ๐๏ธ <class 'dict'> print(dictionary['first']) # ๐๏ธ bobby print(dictionary['last']) # ๐๏ธ hadz
The dict() class can be passed an iterable of keyword arguments or an iterable
of key-value pairs and returns a new dictionary.
If you have a nested OrderedDict object, use the json module instead.
To convert a nested OrderedDict to a regular dict:
json.dumps() method to convert the nested OrderedDict to a JSON
string.json.loads() method to convert the JSON string to a native Python
dictionary.from collections import OrderedDict import json ordered_dict = OrderedDict( [('name', 'bobbyhadz'), ('address', OrderedDict([('post_code', 123)])), ] ) json_str = json.dumps(ordered_dict) regular_dict = json.loads(json_str) # ๐๏ธ {'name': 'bobbyhadz', 'address': {'post_code': 123}} print(regular_dict) print(type(regular_dict)) # ๐๏ธ <class 'dict'>

The first step is to use the json.dumps() method to convert the nested
OrderedDict object to a JSON string.
from collections import OrderedDict import json ordered_dict = OrderedDict( [('name', 'bobbyhadz'), ('address', OrderedDict([('post_code', 123)])), ] ) json_str = json.dumps(ordered_dict) print(json_str) # ๐๏ธ {"name": "bobbyhadz", "address": {"post_code": 123}} print(type(json_str)) # ๐๏ธ <class 'str'>
The json.dumps() method converts a Python object to a JSON formatted string.
json.loads() method to parse the JSON string into a native Python dict.from collections import OrderedDict import json ordered_dict = OrderedDict( [('name', 'bobbyhadz'), ('address', OrderedDict([('post_code', 123)])), ] ) json_str = json.dumps(ordered_dict) regular_dict = json.loads(json_str) # ๐๏ธ {'name': 'bobbyhadz', 'address': {'post_code': 123}} print(regular_dict) print(type(regular_dict)) # ๐๏ธ <class 'dict'>
The json.loads method parses a JSON string into a native Python object.
We can't directly pass a nested OrderedDict to the dict() class because the
dict() class expects an iterable of keyword arguments or an iterable of
key-value pairs.
my_dict = dict( [('id', 1), ('first', 'bobby'), ('last', 'hadz')] ) print(my_dict) # ๐๏ธ {'id': 1, 'first': 'bobby', 'last': 'hadz'}
The
OrderedDict
class is a subclass of dict that remembers the order in which its entries were
added.
Note that as of Python 3.7, the standard dict class is guaranteed to preserve
the insertion order as well.
To convert a dict to an OrderedDict:
dict object to the OrderedDict class if you use a Python version
of 3.7+.3.7.from collections import OrderedDict my_dict = { 'first': 'bobby', 'last': 'hadz', 'age': 30, } # โ Convert dict to OrderedDict (Python version older than 3.7) ordered_keys = ['first', 'last', 'age'] ordered_dict = OrderedDict( [(key, my_dict[key]) for key in my_dict] ) # ๐๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(ordered_dict) # ------------------------------------------------ # โ Convert dict to OrderedDict (Python version 3.7 +) ordered_dict = OrderedDict(my_dict) # ๐๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(ordered_dict)

In the first example, we ordered the keys of the dict in a list.
The
OrderedDict
class is a subclass of dict that remembers the order in which its entries were
added.
dict class is guaranteed to preserve the insertion order as well.However, in versions older than 3.7, the insertion order of dict objects is
not preserved.
We used a list comprehension to iterate over the ordered list of keys.
from collections import OrderedDict my_dict = { 'first': 'bobby', 'last': 'hadz', 'age': 30, } ordered_keys = ['first', 'last', 'age'] list_of_tuples = [(key, my_dict[key]) for key in my_dict] # ๐๏ธ [('first', 'bobby'), ('last', 'hadz'), ('age', 30)] print(list_of_tuples) ordered_dict = OrderedDict(list_of_tuples) # ๐๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(ordered_dict)
On each iteration, we return a tuple containing the current key and the corresponding value.
The insertion order for OrderedDict objects is preserved, so the last step is
to pass the list of key-value pairs to the OrderedDict class.
dict object to the OrderedDict classIf you use a version of Python greater than 3.7, you can directly pass the dict
object to the OrderedDict class as the insertion order for dict objects is
preserved.
from collections import OrderedDict my_dict = { 'first': 'bobby', 'last': 'hadz', 'age': 30, } ordered_dict = OrderedDict(my_dict) # ๐๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('age', 30)]) print(ordered_dict)
Passing the dictionary to the OrderedDict class works even if the dictionary
is nested.
from collections import OrderedDict my_dict = { 'first': 'bobby', 'last': 'hadz', 'address': { 'post_code': 123 } } ordered_dict = OrderedDict(my_dict) # ๐๏ธ OrderedDict([('first', 'bobby'), ('last', 'hadz'), ('address', {'post_code': 123})]) print(ordered_dict)
To convert an OrderedDict to a list:
dict.items() method to get a view of the dictionary's items.list() class to convert the view object to a list.from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) list_of_items = list(my_dict.items()) # ๐๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(list_of_items) list_of_keys = list(my_dict.keys()) print(list_of_keys) # ๐๏ธ ['name', 'age', 'topic'] list_of_values = list(my_dict.values()) print(list_of_values) # ๐๏ธ ['bobbyhadz', 30, 'Python']
The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).
list() class to convert the view object to a list.from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) list_of_items = list(my_dict.items()) # ๐๏ธ [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] print(list_of_items) print(list_of_items[0]) # ๐๏ธ ('name', 'bobbyhadz') print(list_of_items[0][0]) # ๐๏ธ name print(list_of_items[0][1]) # ๐๏ธ bobbyhadz
You can use the dict.keys() method if you need to get a view object of the
keys of the OrderedDict.
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) list_of_keys = list(my_dict.keys()) print(list_of_keys) # ๐๏ธ ['name', 'age', 'topic'] print(list_of_keys[0]) # ๐๏ธ name print(list_of_keys[1]) # ๐๏ธ age
The dict.keys() method returns a new view of the dictionary's keys.
Make sure to convert the view object to a list before accessing it at a specific index.
There is also a dict.values() method if you need to get a view object of the
values of the OrderedDict.
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) list_of_values = list(my_dict.values()) print(list_of_values) # ๐๏ธ ['bobbyhadz', 30, 'Python'] print(list_of_values[0]) # ๐๏ธ bobbyhadz print(list_of_values[1]) # ๐๏ธ 30
The dict.values method returns a new view of the dictionary's values.
You can learn more about the related topics by checking out the following tutorials: