|
| 1 | +#dictionary is an unordered collection of data in the form key:value pairs. |
| 2 | +d={101:'Amit',102:'Anuj',103:'Ajay',104:'Anubhav',105:'Atul'} |
| 3 | +print(type(d)) |
| 4 | +print(d) |
| 5 | +#imp point about dictionary type: |
| 6 | +#1)Ordering is not preserved |
| 7 | +#2)dictionary keys must be unique |
| 8 | +#3)duplicate values are allowed but not keys |
| 9 | +#4)dictionary is mutable |
| 10 | +#5)dictionary is hetrogeneous |
| 11 | +#6)indexing not allowed |
| 12 | +#7)slicing operation not allowed |
| 13 | + |
| 14 | +k=d.keys() |
| 15 | +print(type(k)) |
| 16 | +print(k) |
| 17 | +v=d.values() |
| 18 | +print(type(v)) |
| 19 | +print(v) |
| 20 | + |
| 21 | +#add new element |
| 22 | +#d[key]=value |
| 23 | +d[106]='Yuvraj' |
| 24 | +print(d) |
| 25 | + |
| 26 | +#Update value |
| 27 | +#d[oldkey]=newvalue |
| 28 | +d[103]='Anurag' |
| 29 | +print(d) |
| 30 | + |
| 31 | +#delete element |
| 32 | +del d[102] |
| 33 | +print(d) |
| 34 | +#del d |
| 35 | +#delete using pop() |
| 36 | +d.pop(104) #d.pop(key).....pop() returns the value deleted |
| 37 | +print(d) |
| 38 | + |
| 39 | +print("***Case-1***") |
| 40 | +for x in d: |
| 41 | + print(x) |
| 42 | + |
| 43 | +print("***Case-2***") |
| 44 | +for x in d: |
| 45 | + print(d[x]) |
| 46 | + |
| 47 | +print("***Case-3***") |
| 48 | +for x in d.items(): |
| 49 | + print(x) |
| 50 | + print(x[0]) |
| 51 | + print(x[1]) |
| 52 | + print(type(x)) |
| 53 | + |
| 54 | +print("***Case-4***") |
| 55 | +for keys,values in d.items(): |
| 56 | + print(keys,values) |
| 57 | + |
| 58 | + |
| 59 | +#*****************Dictionary fromekeys() method***************** |
| 60 | +marks={} #Empty Dictionary |
| 61 | +marks = marks.fromkeys(['Math','English','Science'], 0) |
| 62 | +print(marks) |
| 63 | + |
| 64 | +for item in marks.items(): |
| 65 | + print(item) |
| 66 | + |
| 67 | +#list contains keys in sorted order |
| 68 | +l=list(sorted(marks.keys())) |
| 69 | +print(l) |
| 70 | + |
| 71 | +print() |
| 72 | + |
| 73 | +#shallow copy just like list |
| 74 | +dold = {1:'one', 2:'two'} |
| 75 | +dnew = dold |
| 76 | +print(id(dold),id(dnew)) |
| 77 | +print('Orignal Dictionary: ', dold) |
| 78 | +print('New Dictionary: ', dnew) |
| 79 | + |
| 80 | + |
| 81 | +print() |
| 82 | +#***************Dictionary copy() method************************ |
| 83 | +#deep copy or true copy just like list |
| 84 | +#use copy() method to copy dictionary...since dictionary is mutable |
| 85 | +dold = {1:'one', 2:'two'} |
| 86 | +dnew = dold.copy() |
| 87 | +print(id(dold),id(dnew)) |
| 88 | + |
| 89 | +print('Orignal Dictionary: ', dold) |
| 90 | +print('New Dictionary: ', dnew) |
| 91 | + |
| 92 | +''' |
| 93 | +When copy() method is used, a new dictionary is created which is filled with a copy of the references from the original dictionary. |
| 94 | +When = operator is used, a new reference to the original dictionary is created. |
| 95 | +''' |
| 96 | + |
| 97 | + |
| 98 | +#******************Dictionary get() method*********************** |
| 99 | +#The get() method returns the value for the specified key if key is in dictionary. |
| 100 | +#The get() method takes maximum of two parameters: |
| 101 | +#key - key to be searched in the dictionary |
| 102 | +#value (optional) - Value to be returned if the key is not found. The default value is None. |
| 103 | + |
| 104 | +''' |
| 105 | +The get() method returns: |
| 106 | +-->the value for the specified key if key is in dictionary. |
| 107 | +-->None if the key is not found and value is not specified. |
| 108 | +-->value if the key is not found and value is specified. |
| 109 | +''' |
| 110 | +student = {101: 'Amit', 102: 'Anuj'} |
| 111 | + |
| 112 | +print('Name: ', student.get(101)) |
| 113 | +print('Name: ', student.get(102)) |
| 114 | + |
| 115 | +# value is not provided |
| 116 | +print('Name: ', student.get(103)) |
| 117 | + |
| 118 | +# value is provided |
| 119 | +print('Name: ', student.get(104, 0.0)) |
| 120 | + |
| 121 | +''' |
| 122 | +The get() method returns a default value if the key is missing. |
| 123 | +
|
| 124 | +However, if the key is not found when you are using dict[key], KeyError exception is raised. |
| 125 | +''' |
| 126 | + |
| 127 | +#**********************update() method************************** |
| 128 | +''' |
| 129 | +The method update() adds one dictionary key-values pairs in to the other dictionary. This function does not return anything. |
| 130 | +
|
| 131 | +The update() method adds element(s) to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value. |
| 132 | +''' |
| 133 | + |
| 134 | +s1 = {101: 'Amit', 102: 'Anuj'} |
| 135 | +s2 = {101: 'Abhishek', 103: 'Yuvraj'} |
| 136 | +s1.update(s2) |
| 137 | +print(s1) |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments