Dictionary
09 November 2025 05:00 PM
Dictionary is declared by using curly Brackets and elements are in the form of
key : value pairs.
Key and value is separated using colan : and key value pair is separated by using cama ,
Dictionary is Ordered data type.
Properties of keys
Keys can be single value data or immutable data.
D={ 'a':40 , 'b':10 , 'a':50}
D={'a':50:,'b':10}
Duplicate keys are not allowed ,if user tryes to give Duplicate key,the old value will be replaced
with the new value.
Properties of values
Values can be any type of data
Duplicate values are allowed
>>D={}
>>>type(D)
<class 'dict'>
>>>D={'a':234,'b':234,'b':False,'c':100}
>>>len(D)
3
>>>D
{'a': 234, 'b': False, 'c': 100}
>>>D={'a': 234,[33,44,22], 'b': False, 'c': 100}
SyntaxError: ':' expected after dictionary key
>>>D={'a': 234,[33,44,22]: False, 'c': 100}
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
D={'a': 234,[33,44,22]: False, 'c': 100}
TypeError: unhashable type: 'list'
>>>D={'a':40,'b':10,'a':50}
>>>D
{'a': 50, 'b': 10}
Index is not present for dictionary,but values can be accesed with the help of keys.
Varname[key value]
Dictionary is mutable data type,
D['a']=400
↓ ↓
Key value
D={'a':'400','b':[55,33,77],(3,4,5):420}
>>D={'a':40,'b':10,'a':50}
>>>D
New Section 1 Page 1
>>>D
{'a': 50, 'b': 10}
>>>D={'a':'1234','b':[55,33,77],(3,4,5):400}
>>>D
{'a': '1234', 'b': [55, 33, 77], (3, 4, 5): 400}
>>>D
{'a': '1234', 'b': [55, 33, 77], (3, 4, 5): 400}
>>>D[0]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
D[0]
KeyError: 0
>>>D['b']
[55, 33, 77]
>>>D['a']
'1234'
>>>D
{'a': '1234', 'b': [55, 33, 77], (3, 4, 5): 400}
>>>D['c']=24
>>>D
{'a': '1234', 'b': [55, 33, 77], (3, 4, 5): 400, 'c': 24}
>>>D['b']=400
>>>D
{'a': '1234', 'b': 400, (3, 4, 5): 400, 'c': 24}
New Section 1 Page 2