Sitemap

OrderedDict is a dictionary subclass that maintains the order that keys were added. This can be useful if we want to have a consistent order for the elements in the dictionary, rather than having the order change based on the implementation of the dictionary.

To initialize an OrderedDict, we can pass it a list of tuples, with each tuple containing a key-value pair:

from collections import OrderedDict

# Initialize an OrderedDict with a list of tuples
d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3)])

We can also initialize an empty OrderedDict and add key-value pairs using the update method:

from collections import OrderedDict

# Initialize an empty OrderedDict
d = OrderedDict()

# Add key-value pairs using the update method
d.update({'a': 1, 'b': 2, 'c': 3})

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3)])

To access a value in an OrderedDict, we can use the same syntax as a regular dictionary:

from collections import OrderedDict

d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Get the value for key 'b'
value = d['b']

# Print the value
print(value)
# Output: 2

To insert a new key-value pair into an OrderedDict, we can use the update method or the square brackets notation:

from collections import OrderedDict

d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Insert a new key-value pair using the update method
d.update({'d': 4})

# Insert a new key-value pair using the square brackets notation
d['e'] = 5

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

To delete a key-value pair from an OrderedDict, we can use the pop method or the del statement:

from collections import OrderedDict

d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

# Delete the key-value pair for key 'c' using the pop method
d.pop('c')

# Delete the key-value pair for key 'e' using the del statement
del d['e']

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('d', 4)])

To update the value for a particular key in an OrderedDict, we can use the square brackets notation:

from collections import OrderedDict

d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Update the value for key 'b'
d['b'] = 20

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 20), ('c', 3)])

In terms of space complexity, an OrderedDict uses more memory than a regular dictionary because it needs to store the order of the keys in addition to the keys and values themselves.

Get M Haque’s stories in your inbox

Join Medium for free to get updates from this writer.

In terms of time complexity, most operations on an OrderedDict have the same time complexity as a regular dictionary. The main exception is the popitem method, which has a time complexity of O(1) for a regular dictionary but O(n) for an OrderedDict. This is because popitem removes the last key-value pair from the dictionary, and for an OrderedDict, the last key-value pair is not necessarily at the end of the dictionary in memory.

Here are a few more examples of using OrderedDict:

from collections import OrderedDict

# Initialize an OrderedDict with a list of tuples
d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Print the dictionary
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3)])

# Access a value using the key
value = d['b']
print(value)
# Output: 2

# Insert a new key-value pair
d['d'] = 4
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

# Update the value for a key
d['d'] = 40
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 40)])

# Delete a key-value pair
del d['c']
print(d)
# Output: OrderedDict([('a', 1), ('b', 2), ('d', 40)])

# Iterate over the keys and values in the dictionary
for key, value in d.items():
print(key, value)
# Output:
# a 1
# b 2
# d 40

--

--