Python OrderedDict

Data structures are the essential components of any programming language that store and manage the data efficiently. Python provides many built-in data structures, i.e., lists, tuples, and dictionaries, that help the programmers to create efficient applications. The Python dictionaries store the data in key-value pairs. The OrderedDict is the subclass of the dict class and maintains the order of the keys in which were inserted in. This is the one and the only difference between the dict and OrderDict. The dict does not maintain the key’s order.

The OrderedDict keeps the order of keys insertion, and when we iterate through the OrderedDict, then it returns the keys in the same order. On the other hand, when the iteration is performed on dict, the keys are returned in random order. However, the dictionaries are now ordered in Python 3.6 and above versions and return the values in the same order as they are inserted. The OrderedDict class exists in the collections module. Therefore, to use the OrderedDict class, first, import the collections module. This article explains the Python OrderedDict in detail with examples.

Examples

Let’s create an OrderedDict in our Python script and add the data in the form of keys and values. The items() is the built-in Python function that returns the keys and values information.

#importing the collections module

import collections

#creating an OrderedDict

my_dict = collections.OrderedDict()

my_dict["A"]=1

my_dict["B"]=2

my_dict["C"]=3

my_dict["D"]=4

#declaring a for loop to iterate through the OrderDict

for i in my_dict.items():

   print(i)

Output

In the output, it can be observed that the OrderedDict returns the keys and values in the same as inserted.

Now let’s create another OrderDict of a student and store the student information in it.

#importing the collections module

import collections

#creating an OrderedDict of student

std_dict = collections.OrderedDict()

std_dict['name']='John'

std_dict['age']=23

std_dict['email']='john@example.com'

std_dict['class']='BS'

#declaring a for loop to iterate through the OrderDict

for i in std_dict.items():

   print(i)

Output

Changing the values of keys

We can change the value of the specific key of OrderedDict. If we change the value of any particular key, the order of the keys will remain the same in the OrderDict. Let’s change the age value in the student OrderDict.

#importing the collections module

import collections

#creating an OrderedDict of student

std_dict = collections.OrderedDict()

std_dict['name']='John'

std_dict['age']=23

std_dict['email']='john@example.com'

std_dict['class']='BS'

print("The order of the keys before changing the age value:")

#declaring a for loop to iterate through the OrderDict

for i in std_dict.items():

   print(i)

#changing the age value

std_dict['age']=25

print("The order of the keys after changing the age value:")

for i in std_dict.items():

   print(i)

Output

The output shows that the order of the keys remains the same when we change the value of a particular key.

Deleting the value from OrderedDict

Deleting the values from the OrderDict does not change the order of the keys. If we delete a particular key and re-insert it, then it will be added at the end. Let’s see an example of it. We will delete the ‘age’ key from the student dictionary and then re-insert it.

#importing the collections module

import collections

#creating an OrderedDict of student

std_dict = collections.OrderedDict()

std_dict['name']='John'

std_dict['age']=23

std_dict['email']='john@example.com'

std_dict['class']='BS'

print("The order of the keys before changing the age value:")

#declaring a for loop to iterate through the OrderDict

for i in std_dict.items():

   print(i)

#deleting the age key

del std_dict['age']

print("The order of the keys after deleting the age key:")

for i in std_dict.items():

   print(i)

#re-inserting the age key and value

std_dict['age']=23

print("The order of the keys after re-inserting the age key:")

for i in std_dict.items():

   print(i)

Output

In the output, it can be seen that when we re-insert a key, it gets inserted at the end.

Conclusion

The OrderedDict is the subclass of dict that arranges the key in sequence and preserves the order as they are inserted. The OrderedDict class is part of the collections module. This article explains OrderedDict, and it’s functionality with examples.



from Linux Hint https://ift.tt/2J6TGXV

Post a Comment

0 Comments