Connect with us

Hi, what are you looking for?

Data Science

Collections Part Two In Python Programming

Collections Part Two In Python Programming
Collections Part Two In Python Programming

We are in the 2nd part of the collection module series, we will learn a little more features in this section and we will continue to learn the important features before moving on to the last section.

In this section, we will learn the concepts of Deque, UserDict, UserList, UserString. We will also give examples of these concepts.

Deque In Collections

The Deque class offers features that make adding and removing operations easier, thanks to this class you can perform smarter object addition and removal.

from collections import deque

x = deque(["xyz","abc","asd"]) # Syntax

Now we will see two useful functions for adding operations. Which will be “append” and “appendleft”, which are also in the same lists, let’s use them in the code.

x.append("a") # Adding in End
x.leftappend("b") # Adding in First Index
Output:
deque(["xyz","abc","asd","a"])
deque(["b" , "xyz" , "abc" , "asd" , "a"])

Of course, this is not our only feature, let’s examine the functions used to delete objects.

x.pop() # Delete Element In End Index
x.leftpop() # Delete Element In First Index

UserDict In Collections

UserDict is a dictionary-like container that acts as a wrapper around the dictionary objects. This container is used when someone wants to create their own dictionary with some modified or new functionality.

You can see this object as a protection tool, you can change the functioning of the functions used with the dictionaries yourself, and even disable some functions.

from collections import UserDict 

class Dict(UserDict):
   def pop(self, s = None):
       print("Disabled") # You can change this code

x = Dict({"a": 1 , "b": 2 , "c": 4})
x.pop("a")
Output:
Disabled

UserList In Collections

UserList is a list like container that acts as a wrapper around the list objects. This is useful when someone wants to create their own list with some modified or additional functionality.

Yes, the next ones to be examine are classes that have the same properties but affect different objects.

from collections import UserList  
     
class List(UserList):  
    def AddCenter(self, adding):
        count = -1
        for i in self:
            count += 1
        
        self[int(count / 2)] = adding

 
d = List([1 , 2 , 3 , 4 , 5]) 
    
d.AddCenter("x")
print(d)
[1, 2, 'x', 4, 5]

Yes, the good side of these objects is to be able to create your own functions. I wrote a function that I will use when I want to add value to myself, you can write your own functions.

UserString In Collections

Yes, we now understand the concepts such as UserList, UserDict, besides, UserString performs the same task, the only difference is that it takes a String value.

from collections import UserString  
     
class String(UserString):  
    def add(self , s = ""):
        self.data += s

x = String("")
x.add("Welcome to the My Master Designer")

print(x)
Output:
Welcome to the My Master Designer

Click to comment

Leave a Reply

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

You May Also Like

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...

Data Science

SQL (Structured Query Language) is one of the most used tools by data scientists, data analysts, and data engineers. Almost all companies use a...