DICTIONARY DATA OPERATIONS: =========================== WHAT IS DICTIONARY: ================== STRING, LIST, TUPLE, SET ==> GROUP OF ELEMENTS '123', [1,2,3,4], (1,2,3,4), {1,2,3,4} DICTIONARY IS A COLLECTION OF ITEMS ITEMS ==> COMBINATION OF KEY AND VALUE PAIR Syntax: key : value CAN DEFINE WITH {} Syntax: Identifier for dictionary object = {key1:value1,key2:value2,key3:value3} Dictionary Properties: ====================== 1) It is a collection of key and value pairs 2) It is an inbuilt datatype predefined class: dict type() 3) Keys in a dictionary with any datatype. 4) Values in a dictionary with any datatype. Note: Keys in a dictionary always be with primitive datatypes. if collection data as key in a dictionary ==> Type Error. 5) To access the dictionary values, we can use "keys". Syntax: dictionary-object-name[key-name] 6) If indexing can use to access the data of dictionary ==> "Key error". If that specified index is not as a key in a dictionary. 7) Can be ordered. 8) Dictionary can be homogeneous and heterogeneous also. homogeneous ==> when keys with same type and values with same type heterogeneous when keys ==> different type values ==> different type 9) Mutable datatype. 10) Keys are cannot be duplicated. 11) Values can be duplicated. # dictionary properties d1 = {} # empty dictionary d2 = {1,2,3,4} d3 = {11:True, 12 : False, 13 : 112233} d4 = {'a' : 11, 'b' : 12, 'c' : 13} d5 = {'abc' : {1,2,3},'def' : [2,3,4],'ghi' : (9,7,8,6)} # d6 = {[1,2,3] : 'a',(1,4,7) : 1122,{1,3,5} : 'pqrs'} d6 = {1 : 10,2 : 20,3 :30} d7 = {True:11,'a' : 13,1-2j : 15} # Heterogeneous d8 = {'a' : 11,'b' : 111, 'c' : 1111} # Homogeneous print(type(d1));print(type(d2));print(type(d3),type(d4),type(d5)) print(d1);print(d2);print(d3);print(d4);print(d5) # values with keys print(d3[12]);print(d4['b']);print(d5['ghi']) print(d6[1]);print(d6[2]) # print(d6[-1]) print(d6) print(type(d7));print(type(d8)) print(d8) print(id(d8)) d8['c'] = 9797 print(d8) print(id(d8)) d8['p'] = 112233 print(d8) print(id(d8)) ============================================= Creation of Dictionary data: ============================ 1) Compile time definition: =========================== ==> direct value assignment method Syntax: dictionary-object-name = {key1:value1,key2:value2,...} # Compile Time Definition of the dictionary d1 = {'a' : 100,'b' : 200, 'c' : 300,'b' : 500,'d' : 300} print(d1) print(type(d1)) ============================================================ 2) Run Time definition: ======================= eval() ==> can use. ==> this definition can dynamically changed. Syntax: dictionary-object-name = eval(input("Enter a dictionary data:")) # Run Time Definition of the Dictionary d1 = eval(input("Enter a dictionary data:")) print(type(d1)) print(d1) ============================================ 3) using dict() =============== Syntax: dictionary-name = dict({key1:value1,key2:value2}) # using dict() method d1 = dict() # empty dictionary d2 = dict({'a' : 11,'b' : 22,'c' : 33}) # d3 = dict((1,3,5,7,9)) print(type(d1));print(type(d2)) print(d1);print(d2) ============================================================ Traversing on Dictionary: ========================= # WAP TO ENTER NAME AND PERCENTAGE IN A DICTIONARY AND DISPLAY THAT INFORMATION. record = dict() # created an empty dictionary count = int(input("Enter the size of the class:")) i = 1 # initialization while i <= count: name = input("Enter a student name:") marks = input("Enter student's percentage:") record[name] = marks i += 1 print("Name of the Student","\t","% of marks of the Student") for data in record: print("\t\t",data,"\t\t\t\t\t",record[data]) ============================================================== Delete data from dictionary data: ================================= 1) using del property: ====================== # delete operation d1 = {'abc' : 111, 'def' : 222, 'ghi' : 333, 'jkl' : 444} # del property can use to delete the value of the dictionary with 'key # Syntax: del dict-name[key] print("Before The Delete Operation:",d1) del d1['ghi'] print("After The Delete Operation:",d1) # we can delete the whole dictionary permanently. del d1 # print(d1) ======================================= 2) clear() ========= it can use to delete/remove the whole dictionary. Syntax: dict-name.clear() # clear() Method d1 = {'abc' : 111, 'def' : 222, 'ghi' : 333, 'jkl' : 444} print("The Dictionary before the delete operation:",d1) d1.clear() print("The Dictionary after the delete operation:",d1) ====================================== 3) pop() ======== we can use to remove the value of the dictionary which is associated with the specified key. Syntax: dict-name.pop(key) Note: ==== if the specified key is not in the dictionary ==> Key Error # pop() method d1 = {'abc' : 111, 'def' : 222, 'ghi' : 333, 'jkl' : 444} print("Before the pop operation:",d1) d1.pop('ghi') # d1.pop('pqr') Key Error print("After the pop operation:",d1) ===================================================== 5) popitem() ============ it can use to remove an arbitrary item {key and value} from the last of the dictionary. Syntax: dict-name.popitem() # popitem() d1 = {'abc' : 111, 'def' : 222, 'ghi' : 333, 'jkl' : 444} print("The Dictionary before the pop operation is: ",d1) d1.popitem() d1.popitem() print("The Dictionary after the pop operation is: ",d1) ===============