DICTIONARY OPERATIONS: ======================= DICTIONARY ALIASING ==================== WHEN WE CREATE A COLLECTION FROM ANOTHER COLLECTION USING '=' AFTER THAT: A CHANGE ON ONE COLLECTION WILL REFLECT ANOTHER COLLECTION THIS PHENOMENA IS CALLED AS "ALIASING". OPERATOR1 = OPERATOR2 USER CALL FOR 3MIN CALL FOR 3MIN VIDEO CALL VIDOE CALL # Dictionary Aliasing d = eval(input("Enter a dictionary:")) dc = d # from d there is a new dictionary is created print(id(d));print(id(dc)) print(d);print(dc) d['papaya'] = 600 dc['cherry'] = 1000 print(id(d));print(id(dc)) print(d);print(dc) ============================================ Dictionary Cloning ================== copying we can create a dictionary from another dictionary by performing copy operation after that: the change on one dictionary will not reflect on another. copy() ===== is an inbuilt method which we can use to copy the data of one dictionary to another dictionary. Syntax: new-dict = old-dict.copy() d = eval(input("Enter a dictionary:")) dc = d.copy() print(id(d));print(id(dc)) print(d);print(dc) d['e'] = 12345 dc['a'] = 9779 print(id(d));print(id(dc)) print(d);print(dc) ================================================= Accessing of only Keys from the dictionary: =========================================== # WAP TO CREATE THE DICTIONARY AND PRINT ALL THE KEYS IN A DICTIONARY. d = eval(input("Enter a dictionary:")) print("All Keys in the dictionary are:") for keys in d: print(keys,end = "\t") print() ======================= keys() ====== ==> an inbuilt method, which we can use to access only the keys of the dictionary Syntax: dict-name.keys() d = {'apple' : 100, 'banana' : 150, 'cherry' : 200, 'papaya' : 250} print("The Dictionary Keys are:") print(d.keys()) for key in d.keys(): print(key,end = "\t") print() ========================================================== Accessing of only values from the dictionary: ============================================== values(): ========= is an inbuilt method, which we can use to access only the values from the dictionary. Syntax: dict-name.values() # WAP TO DEFINE A DICTIONARY AND PRINT ONLY THE VALUES OF THE DICTIONARY. d = {'a' : 11, 'b' : True, 'c' : 1-2j, 'd' : 0b11001,'e' : 'python'} print("The Values of the dictionary are:") for v in d: print(d[v],end = "\t") print() print(d.values()) for val in d.values(): print(val,end = "\t") ========================================================== Accessing of items from the dictionary: ========================================= item ==> an arbitrary of the dictionary key:value d = {'a' : 1, 'ab' : 11, 'abc' : 111, 'abcd' : 1111} for k in d: res = (k,d[k]) print(res,",",end = "") print() items() ======= is an inbuilt method, which we can use to access an arbitrary of the dictionary. Syntax: dict-name.items() d = {'a' : 1, 'ab' : 11, 'abc' : 111, 'abcd' : 1111} print(d.items()) for k,v in d.items(): res = (k,v) print(res,",",end = "") ============================================== Comprehensions =============== it is very easy and compact way of creating collections from any iterable objects like: list, tuple, set, dict, range() based on some conditions. Syntax: object-name = [condition for lv in iterable-object] object-name = {condition for lv in iterable-object} object-name = {key:condition for value for key in iterablae-object} # creation of list using iteration a = list() for i in range(1,100): if i % 9 == 0: a.append(i) print(a) # List Comprehension b = [x * x for x in range(1,11)] c = [j * 6 for j in (1,3,5,7,9)] print(b);print(c) print(type(b)) # Tuple Comprehension ==> Not possible in python d = (y % 2 for y in {11,12,13,14,15,16}) print(d);print(type(d)) # Set Comprehension e = {z % 3 for z in range(10)} print(type(e));print(e) # Dictionary Comprehension f = {p : p**3 for p in range(6)} print(type(f));print(f)