w3resource

Python OrderedDict exercises with solutions


This resource offers a total of 50 Python OrderedDict Data Type problems for practice. It includes 10 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

Python OrderedDict is a dict subclass that remembers the order in which its items were inserted. When we iterate over an OrderedDict, the items will be returned in the order they were inserted, not in some arbitrary order.

The main features of OrderedDict are:

  • Unlike regular dictionaries, an OrderedDict remembers the order in which items were inserted.
  • When iterating through an OrderedDict, the items are returned in the order they were inserted.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. OrderedDict Creation

Write a Python program that creates an OrderedDict and adds some items. Print the OrderedDict contents.

Click me to see the sample solution


2. Sort OrderedDict

Write a Python program that sorts the OrderedDict by its keys. Sort the OrderedDict by its values as well.

Click me to see the sample solution


3. Access and Check in OrderedDict

Write a Python program that accesses an item in the OrderedDict by its key. Check if a specified item exists in the OrderedDict as well.

Click me to see the sample solution


4. Reverse OrderedDict

Write a Python program that reverses the order of a given OrderedDict.

Click me to see the sample solution


5. Move Key to End

Write a Python program to create an OrderedDict with the following key-value pairs:

'Laptop': 40
'Desktop': 45
'Mobile': 35
'Charger': 25
Now move the 'Desktop' key to the end of the dictionary and print the updated contents.

Click me to see the sample solution


6. Remove First Key-Value Pair

Write a Python program to create an OrderedDict with the following key-value pairs:

'Laptop': 40
'Desktop': 45
'Mobile': 35
'Charger': 25
Now remove the first key-value pair and print the updated OrderedDict.
Click me to see the sample solution


7. Merge OrderedDicts

Write a Python function that merges two OrderedDict objects. If there are duplicate keys, sum their values. Print the merged OrderedDict.

Click me to see the sample solution


8. Remove Key from OrderedDict

Write a Python function that creates an OrderedDict and removes a key-value pair from the OrderedDict using a given key.

Click me to see the sample solution


9. OrderedDict from Random Data

Write a Python program that creates an OrderedDict and populates it with random integer values as values and their ASCII characters as keys. Print the OrderedDict.

Click me to see the sample solution


10. Words to Length Mapping

Write a Python function that takes a list of words and returns an OrderedDict where keys are the words and values are the lengths of the words.

Click me to see the sample solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.