Collection Module In Python – Final Part

We are in the last part of the tutorial we created for the collection module, in this section we will learn about OrderedDict, Defaultdict, also we will repeat old topics and reinforce what we have learned with the project.

Since the training content is still full, you can learn the order of the topics from the list below. Enjoyable learnings

Topics to Learn

  1. OrderedDict In Collections
  2. Defaultdict In Collections
  3. Making Project With New Subject
  4. Summary Collections
  5. Last Word

OrderedDict In Collections

The only difference with a normal dictionary is the saving order of the data, so the order in which you saved the data will be shown to you in the same way.

As an example, let’s set a value to OrderedDict with a normal dictionary and check the output. Thus, we can learn the difference between the two classes more easily.

from collections import OrderedDict as OD

x = {}
x["1"] = "x"
x["2"] = "y"
x["3"] = "z"

y = OD()
y["1"] = "x"
y["2"] = "y"
y["3"] = "z"

for key, value in x.items():
   print(key , value)


for key, value in y.items():
   print(key , value)
1 x
3 z
2 y

1 x
2 y
3 z

DefaultDict In Collections

DefaultDict is a structure used for those who want to reach with the key that the dictonary does not contain.

As an example, you have a Dictionary that holds 3 values, but one of them is trying to reach the 4th, and you can create a function that will be done by default.

We said we can create your own functions, just like the UserDict, we will use the Collections module again for this. Let’s get started.

from collections import defaultdict as default

def x():
    return "Not Found"

dict = default(x)
dict[1] = "a"
dict[2] = "b"

Output: Not Found

Making Project With New Subject

Now is the time to consolidate the topics we have learned, the project we will do will be a project related to dictionaries.

Our project will be a program that saves the books coming to the library, using OrderedDict, we will sort according to the date they arrived.then we will inform the user when a book is searched with DefaultDict.

from collections import defaultdict as default
from collections import OrderedDict as OD

def NotFound():
    return "Not Found Book"

books = OD()

books[1] = "Book 1"
books[2] = "Book 2"
books[3] = "Book 3"
books[4] = "Book 4"
books[5] = "Book 5"

books = default(NotFound , books)

Book 1
Not Found Book

Summary Collections

This module has caught your eye in all 3 pieces of training we learned. Module lists have been created for dictionaries and tuples. They have been developed to cover missing places.

You will use this module frequently in projects, now you can take your work comfortably with lists and dictionaries, but be sure, it is very useful to use these features in projects.

Just before we get to the last word, we will briefly look at what we have learned and what they do. If you have a missing point, you can start reading here again.

Counter Object = to calculate and sort the content of the value contained in it.
ChainMap = It collects your dictionaries in one unit and returns a dictionary list.
NamedTuple = We can call sub-attributes as if calling an object with NamedTuple.
Deque = The Deque class offers features that make adding and removing operations easier
UserDict = It allows you to write function for dictionaries.
UserList = It allows you to write a function for the list.
UserString = It allows you to write a function for the string data type.

Last Word

New trainings will come after the Collections module, we will learn other modules, stay tuned, do not miss – My Master Designer.

Leave a Reply

Your email address will not be published. Required fields are marked *