DICTIONARY DATA OPERATIONS ========================== STRING OPERATION, LIST OPERATIONS TUPLE OPERATIONS SET OPEARTIONS WHAT IS THE DICTIONARY? ======================= 1) COLLECTION DATATYPE THAT MEANS, IT IS THE GROUP OF MORE THAT ONE ELEMENT 2) DICTIONARY IS THE COLLECTION OF ITEMS ITEM ==> PAIR PAIR OF KEY AND VALUE Syntax: key-name : value Syntax for the dictionary is: dictionary-name = {item1, item2, item3, item4,....} dictionary-name = {key1:value1, key2:value2, key3:value3,.......} 3) Inbuilt datatype a pre-defined class => dict # Dictionary Definition # Empty dictionary dict_empty = {} # dictionary with items dict_items = {'a' : 111,'b' : 121,'c' : 131,'d' : 141,'e' : 151} # finding the pre-defined class for the above definition print(type(dict_empty)) print(type(dict_items)) =================================================== Features of the Dictionary: =========================== 1) Non-sequential Data structure. because, it cannot support "index". 2) Ordered Data structure. 3) Mapping Data structure. items key-name : value ==> to access the values of the dictionary, we can use "keys" Syntax: dictionary-name[key-name] 4) About keys: keys are of any datatype. Note: the keys with list ==> Type Error the keys as sets ==> Type Error ==> Keys of dictionary ==> Unique If the keys are duplicated, then the dictionary can update with latest definitions. 5) About Values The values of dictionary ==> any datatype Not a unique 6) Keys with Homogeneous and Heterogeneous elements are accepted Values with Homogeneous and Heterogeneous elements are accepted. 7) Mutable Data structure. # Features of Dictionary # Non-sequential Data structure d = {'a':100,'b':200,'c':300,'d':400,'e':500,0 :600} d1 = {10:'a',20:'b',30:'c',40:'d',50:'e'} d2 = {0.001:'p',0.0001:'q',0.00001:'r',0.000001:'s'} # d3 = {[1,2,3]:'abc',[4,5,6]:'def',[7,8,9]:'ghi'} d3 = {(1,2,3):'abc',(4,5,6):'def',(7,8,9):'ghi'} # d4 = {{1,3,5}:100,{2,4,6}:200} d5 = {'a' : 11,'e':22,'i':33,'o':44,'u':55,'i':66,'a':77} d6 = {'a':111,'e':222,'i':222,'o':111,'u':555} d7 = {'abc':[100,200,300],'def': [200,300,400]} d8 = {'a':111,122:'c',0.001:12-23j,12+23j:True} d9 = {'apple' : 1000,'banana':123.234,'cherry':12-24j,'mango' : True} print(d[0]) # print(d[-1]) # Ordered Data structure print(d) # Mapping type print(d['a']);print(d['b']);print(d['c']);print(d['d']);print(d['e']);print(d[0]) print(type(d1));print(type(d2));print(type(d3)) print(d5) print(d6) print(d7) print(type(d8));print(type(d9)) # Before the change on the dictionary print(d1);print(id(d1)) # After the change on the dictionary d1[100] = 'A' print(d1);print(id(d1)) d[10] = 'B' print(d1);print(id(d1)) d5['a'] = 1223 print(d5) =========================================== Creation of Dictionary: ======================= 1) Compile Time Definition Syntax: dict-name = {key1:value1,key2:value2,key3:value3...} 2) Run Time Definition: eval() Syntax: var_name = eval(input("Enter some Dictionary") 3) using dict() =============== i) using dict(), we can create/define the empty dictionary ii) we can also create the diction with items using dict() Syntax: identifier = dict({key1:val1,key2:val2,key3:val3}) identifier = dict({(key1,val1),(key2,val2),(key3,val3)}) # Rune Time Definition d = eval(input("Enter a dictionary:")) d1 = dict() d2 = dict({'a':(100,200),'e':1221,'i':100}) # d3 = dict({100,200,300,400}) d3 = dict({('a',111),('b',222),('c',333),('d',444)}) # d4 = dict({['a',11],['b',22],['c',33]}) print(type(d));print(type(d1));print(type(d2));print(type(d3)) print(d) print(d1) print(d2) print(d3)