Python_notes_dict
Python_notes_dict
Here Key1,Key2...Key-n are called Values of Key and They must be Unique
Here Val1, Val2...Val-n are called Values of Value and They may or may not be
unique.
=========================================================
Examples:
>>> d1={10:"Python",20:"Data Sci",30:"Django"}
>>> print(d1,type(d1))----------{10: 'Python', 20: 'Data Sci', 30: 'Django'} <class
'dict'>
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2,type(d2))------------{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class 'dict'>
>>> len(d1)--------------3
>>> len(d2)------------4
-------------------------------------------------
>>> d3={}
>>> print(d3,type(d3))------------{} <class 'dict'>
>>> len(d3)-------------0
>>> d4=dict()
>>> print(d4,type(d4))-------------{} <class 'dict'>
>>> len(d4)---------------0
------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2)----------------------------------{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4}
>>> d2[0]----------------------------------------KeyError: 0
>>> d2[10]-------------------------------------3.4
>>> d2[10]=10.44
>>> print(d2)-------------------------------{10: 10.44, 20: 4.5, 30: 5.6, 40: 3.4}
-------------------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2,type(d2),id(d2))----{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class 'dict'>
2090750380736
>>> d2[50]=5.5
>>> print(d2,type(d2),id(d2))---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4, 50: 5.5}
<class 'dict'>
2090750380736
------------------------------------------------------------------------------------------------
>>> d3={}
>>> print(d3,type(d3),id(d3))-------------------{} <class 'dict'> 2090750332992
>>> d3["Python"]=1
>>> d3["Java"]=3
>>> d3["C"]=2
>>> d3["GO"]=1
>>> print(d3,type(d3),id(d3))-----{'Python': 1, 'Java': 3, 'C': 2, 'GO': 1} <class
'dict'>
2090750332992
------------------------------------------------------------------------------------------------
>>> d4=dict()
>>> print(d4,type(d4),id(d4))---------------{} <class 'dict'> 2090754532032
>>> d4[10]="Apple"
>>> d4[20]="Mango"
>>> d4[30]="Kiwi"
>>> d4[40]="Sberry"
>>> d4[50]="Orange"
>>> print(d4,type(d4),id(d4))---{10: 'Apple', 20: 'Mango', 30: 'Kiwi', 40:
'Sberry', 50: 'Orange'} <class
'dict'> 2090754532032
>>> d4[10]="Guava"
>>> print(d4,type(d4),id(d4))----{10: 'Guava', 20: 'Mango', 30: 'Kiwi', 40:
'Sberry', 50: 'Orange'}
<class 'dict'>
2090754532032
--------------------------------------------------------------------------------------
>>> d2={10:3.4,20:4.5,30:5.6,40:3.4}
>>> print(d2,type(d2),id(d2))---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4} <class 'dict'>
2090754531520
>>> d2[50]=1.2
>>> print(d2,type(d2),id(d2))---{10: 3.4, 20: 4.5, 30: 5.6, 40: 3.4, 50: 1.2}
<class 'dict'>
2090754531520
====================================================
Pre-Defined functions in dict
====================================================
=>In dict object, we have the following function for performing Various
Operations.
------------------------------------------------------------------------------------------------
1. clear ()
------------------------------------------------------------------------------------------------
Syntax: dictobj.clear()
=>This Function is used for Removing all the elements of non-empty dict object
=>If we call this function upon empty dict object then we get None / No
Output.
Examples
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,type(d1),id(d1))---{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
<class 'dict'> 2364664861568
>>> d1.clear()
>>> print(d1,type(d1),id(d1))----{} <class 'dict'> 2364664861568
>>> print(d1.clear())----------None
>>> print(dict().clear())---------None
>>> print({}.clear())--------------None
------------------------------------------------------------------------------------------------
2. pop()
------------------------------------------------------------------------------------------------
=>Syntax: dictobj.pop(Key)
------------------------------------------------------------------------------------------------
4. copy()
------------------------------------------------------------------------------------------------
Syntax: dictobj2=dictobj1.copy()
=>This function is used for Copying the content of One dict object into another
dict object.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156416
>>> d2=d1.copy() # Shallow Copy
>>> print(d2,id(d2))----{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> d1[50]="Dsc"
>>> d2[60]="Django"
>>> print(d1,id(d1))----{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669156416
>>> print(d2,id(d2))---{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 60: 'Django'}
2364669156096
----------------
Deep Copy Process of dict objects
---------------------
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669155776
>>> d2=d1 # Deep Coy
>>> print(d2,id(d2))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669155776
>>> d1[50]="Dsc"
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669155776
>>> print(d2,id(d2))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C', 50: 'Dsc'}
2364669155776
------------------------------------------------------------------------------------------------
5. get()
------------------------------------------------------------------------------------------------
Syntax: This Function is used for obtaining Value of Value by Passing Value
of Key.
=>if the value of Key does not exist then we get None as a result
(OR)
Syntax: dictobj[Key]---->Will give Value of Value if Key Present otherwise
get KeyError
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))-------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> v=d1.get(10)
>>> print(v)------------Python
>>> v=d1.get(20)
>>> print(v)-----------Java
#OR
>>> d1.get(10)--------'Python'
>>> d1.get(20)--------'Java'
>>> print(d1.get(200))-------None
>>> v=d1.get(200)
>>> print(v)--------------None
>>> print(dict().get(10))---------None
>>> print({}.get(10))------------None
-------------------------OR----------------------------
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))-----------------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156288
>>> d1.get(10)--------------'Python'
>>> d1[10]--------------------'Python'
>>> d1[20]---------------------'Java'
>>> d1[40]--------------------'C'
>>> d1[400]-------------------KeyError: 400
>>> print(d1.get(400))--------None
------------------------------------------------------------------------------------------------
6. keys()
------------------------------------------------------------------------------------------------
Syntax: varname=dictobj.keys()
(OR)
dictobj.keys()
=>This Function is used for obtaining set of Keys in the form of dict_keys
object.
Examples:
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))----------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156096
>>> ks=d1.keys()
>>> print(ks,type(ks))---------dict_keys([10, 20, 30, 40]) <class 'dict_keys'>
>>> for k in ks:
... print(k)
...
10
20
30
40
>>> for k in d1.keys():
... print(k)
...
10
20
30
40
>>> for k in d1.keys():
... print(k,"-->",d1.get(k))
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
>>> for k in d1.keys():
... print(k,"-->",d1[k])
...
10 --> Python
20 --> Java
30 --> C++
40 --> C
------------------------------------------------------------------------------------------------
7. values()
------------------------------------------------------------------------------------------------
Syntax: varname=dictobj.values()
(OR)
dictobj.values()
=>This Function is used for obtaining set of Values in the form of dict_values
object.
Examples
>>> d1={10:"Python",20:"Java",30:"C++",40:"C"}
>>> print(d1,id(d1))---------{10: 'Python', 20: 'Java', 30: 'C++', 40: 'C'}
2364669156288
>>> vs=d1.values()
>>> print(vs)---------dict_values(['Python', 'Java', 'C++', 'C'])
>>> for v in vs:
... print(v)
...
Python
Java
C++
C
>>> for v in d1.values():
... print(v)
...
Python
Java
C++
C
---------------------------------
MOST IMP
---------------------------------
>>> for x in d1:
... print(x)
...
10
20
30
40
Examples:
>>> t1=(10,"Rossum",{"cm":16,"cppm":17,"pym":18},"OUCET")
>>> print(t1,type(t1))----------(10, 'Rossum', {'cm': 16, 'cppm': 17, 'pym': 18},
'OUCET') <class 'tuple'>
>>> for val in t1:
... print(val,type(val))
...
10 <class 'int'>
Rossum <class 'str'>
{'cm': 16, 'cppm': 17, 'pym': 18} <class 'dict'>
OUCET <class 'str'>
>>> for k,v in t1[-2].items():
... print(k,"-->",v)
...
cm --> 16
cppm --> 17
pym --> 18
dict in set not possible
------------------------------------------------------------------------------------------------
>>> s1={10,"Rossum",{"cm":16,"cppm":17,"pym":18},"OUCET"}--
TypeError: unhashable type: 'dict'
------------------------------------------------------------------------------------------------