DICTIONARY OPERATIONS: ====================== 1) FINDING LENGTH OF THE DICTIONARY: =================================== len(): ===== using len() we can find the length of any collection. Syntax: len(dictionary-name) # WAP TO FIND THE LENGTH OF THE DICTIONARY d = eval(input("Enter a dictionary:")) print("The Length of the dictionary is = ",len(d)) count = 0 for cnt in d: count += 1 print("The Length of the dictionary is = ",count) ================================================ Getting values from the Dictionary: =================================== dict-name[key] ==> values get() ===== we can use get() to access the values of the dictionary. Syntax: dict-name.get(key) Note: ===== 1) If the specified key is in the dictionary ==> associated value can be returned by the get() 2) If the specified key is not in the dictionary ==> None. 3) Instead to return 'None' when the specified key is not in the dictionary to return some default value: Syntax: dict-name.get(key,default-value) d = {'apple' : 10, 'banana' : 15, 'mango' : 25, 'kiwi' : 35, 'papaya' : 20} print(d['mango']) res = d.get('mango') print(res) print(d.get('jackfruit')) print(d.get('jackfruit',0)) ============================================= Getting of Keys from the dictionary: ==================================== keys(): ====== return all the associated keys of dictionary. Syntax: dict-name.keys() Getting of values from the dictionary: ====================================== values(): return all associated values of the dictionary. Syntax: dict-name.values() d = eval(input("Enter a Dictionary:")) keys = d.keys() values = d.values() print("All The Keys of the Dictionary = ",keys) print("All The Values of the Dictionary = ",values) ==================================== # WAP TO PRINT ALL THE KEYS OF THE DICTIONARY d = eval(input("Enter a dictionary:")) for keys in d: print(keys) for k in d.keys(): print(k) ================================ # WAP TO PRINT ALL THE VALUES OF THE DICTIONARY d = dict({'apple' : 100, 'banana' : 125, 'mango' : 200, 'kiwi' : 175}) for values in d: print(d[values]) print() for v in d.values(): print(v) =========================================== Getting both keys and values in arbitrary from the dictionary: ============================================================== items(): ======== Syntax: dict-name.items() return: list of tuples with keys and values ex: [(key1,val1),(key2,val2),(key3,val3)] d = eval(input("Enter a dictionary:")) items = d.items() print(items) for item in d.items(): print(item) ======================================== copy() ====== to create a dictionary from other dictionary, copy() can use. Syntax: new-dict = old-dict.copy() # cloning of the dictionary d = {'name' : 'Kumar', 'age' : 26, 'Course' : 'Python','Duration' : 4} print(id(d)) print(d) cd = d.copy() print(id(cd)) print(cd) cd['name'] = 'sahithi' print(d) print(cd) d['name'] = 'Ashwini' print(d) print(cd) =================================== Aliasing of Dictionary Data: =========================== using assignment operator d = {'a' : 100, 'b' : 200, 'c' : 300} cd = d print(d);print(cd) print(id(d));print(id(cd)) cd['d'] = 400 d['a'] = 111 print(d);print(cd) ======================================== # WAP TO FIND THE NUMBER OF OCCURRENCES OF EACH LETTER PRESENT IN THE GIVEN STRING # 'python' # output: {'p' : 1, 'y' :1, 't' : 1, 'h' : 1, 'o' : 1,'n' :1} # 'apple' # output: {'a' : 1, 'p' : 2, 'l' : 1,'e' :1} data = input("Enter a string data:") # apple d = dict() for cnt in data: d[cnt] = d.get(cnt,0) + 1 for k,v in d.items(): print(k,"occurred",v,"times") ====================================================== Comprehensions =============== very easy and compact way to create collection objects like: list, set, dictionary from any iterable object like: range(), list, tuple etc. based on some condition. Syntax: identifier = [condition for lv in collection/range] ==> List Comprehension identifier = {condition for lv in collection/range} ==> set comprehension identifier = {key : value for condition for lv in collection/range} ==> Dictionary Comprehension Note: Tuple Comprehension is not possible in Python. # dictionary Comprehensions s = {k : k * k for k in range(1,11)} d = {i : i + 2 for i in range(10,16)} d1 = {j : j % 2 == 0 for j in [1,2,3,4,5,6,7,8,9,10]} print(s);print(type(s)) print(d) print(d1) # set comprehensions s1 = {x * x for x in range(6)} print(s1) # Tuple Comprehensions ==> not possible in python t1 = (d * 3 for d in range(10)) print(type(t1)) for i in t1: print(i,end = "\t") print() # List Comprehension ld = [s * 2 for s in range(10)] print(ld)