Data Structures and Algorithms: =============================== Strings and manipulations Lists Tuple Sets Dictionary Frozen Sets Bytes Byte arrays Collection Module: ================== ==> an inbuilt module in python ==> provides an alternative to all inbuilt python datatypes like: list, tuple etc. ==> collection module contains lot of built-in datatypes which are alternatives to above datatypes. 1) counters: ============= ==> subclass of dictionary designed to count the hash able object. ex: ["apple", "orange", "banana", "apple", "mango", "orange", "papaya", "banana", "apple", "kiwi"] ==> "apple" ==> 3 "orange" ==> 2 "banana" ==> 3 "mango" ==> 1 "papaya" ==> "kiwi" ==> 1 # counters from collections import Counter # create the counter # Counter() data = Counter(['apple', 'banana', 'orange', 'apple', 'papaya', 'orange', 'mango', 'papaya', 'kiwi','apple']) print("The Given Counter is :") print(data) # update the counter # update() data.update(['strawberry','grapes']) print(data) # Accessing the elements from the counter # most_common() print(data.most_common(1)) print(data.most_common(0)) print(data.most_common(5)) # print(data[0]) ============================================== OrderedDict: ============ ==> a sub-class of the dictionary that maintains the sequence of items as we were added. ==> gives the facility to add/insert and remove elements. # orderedDict from collections import OrderedDict # create the orderedDict #OrderedDict() data = OrderedDict([('a',100),('b',200),('c',300),('d',400),('e',500)]) print("The Given Ordered Dictionary is = ") print(data) # Insertion of elements into ordered dictionary # update() data.update({'f' : 600}) # data.update((('g',700))) data.update([('g',700)]) data.update({('h',800)}) print(data) # Reversing the order # reversed() res_data = OrderedDict(reversed(list(data.items()))) print("The Reversed Ordered Dictionary = ") print(res_data) # print(reversed(data)) ============================================== DefaultDict: ============ ==> a sub-class of dictionary that calls a factory function to supply missing values, simplifying handling of missing keys. # Default Dict from collections import defaultdict # create the default dict # data = defaultdict(int) # data = defaultdict(float) data = defaultdict(str) print(data) data['a'] = '12.2' data['b'] += '2.34' print(data) ========================================== ChainMap: ========= ==> groups multiple dictionaries into a single view by making it convenient to mange with multiple scopes. from collections import ChainMap d1 = {'a' : 111, 'e' : 222, 'i' : 333, 'o' : 444, 'u' : 555} d2 = {'b' : 100, 'a' : 200, 'p' : 300, 'e' : 400} print(d1) print(d2) result = ChainMap(d1, d2) print(result) =================================================== Namedtuple: =========== # Named Tuple from collections import namedtuple # create the named tuple data = namedtuple('data',['name','quantity']) p1 = data(name = 'a',quantity=121) print(data) print(p1) print(p1[1]) # print(p1['a']) ============================================== Linked List: ============ ==> a linear collection of data elements where each element pointes to the next, allowing for a dynamic data structure with efficient insertion and deletions. # Linked List class Node: def __init__(self,data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def printList(self): temp = self.head while temp: print(temp.data) temp = temp.next lld = LinkedList() lld.head = Node(100) s = Node(200) t = Node(300) f = Node(400) lld.head.next = s s.next = t t.next = f lld.printList()