09-Python Dictionary-11-2024-2025
09-Python Dictionary-11-2024-2025
DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K
Syntax:
PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K PURAM NEW DELHI
COMPUTER{<Index1/Key1>:<Value1>,<Index2/Key2>:<Value2>,...
SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER}SCIENCE DEPT
MK MD DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC
SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K PURAM NEW
Examples of dict Contents:
#Dictionary
S={"Ino":101,"Item":"Pencil"}
M={1:200,2:160,3:180}
T={"Suraj":100,"John":150,"Ahmad":130}
Dictionary 6
CODE
CODE
T=((1,"Amar"),(2,"Geet"),(3,"Neel"))
D=dict(T)
print(D)
OUTPUT
{1: 'Amar', 2: 'Geet', 3: 'Neel'}
dict() - To convert nested list to dictionary 9
CODE
T=[[1,"Amar"],[2,"Geet"],[3,"Neel"]]
D=dict(T)
print(D)
OUTPUT
{1: 'Amar', 2: 'Geet', 3: 'Neel'}
Dictionary - keys() and values() 10
CODE
OUTPUT Elements: 2
K: dict_keys(['Rno', 'Name'])
V: dict_values([10, 'Amar'])
D: {'Rno': 10, 'Name': 'Amar'}
10
Amar
Dictionary - items() 11
CODE
OUTPUT
1
None
2
Not Found !
Dictionary - update() 13
CODE
OUTPUT
{1: [100, 200, 300], 2: [100, 200, 300], 3: [100, 200, 300]}
Dictionary - copy() 19
CODE
OUTPUT
D1 : {'Name': 'John', 'DOB': '2002-02-12'}
D2 : {'Name': 'John', 'DOB': '2002-02-12'}
D1 : {'Name': 'Jiya', 'DOB': '2002-02-12'}
Dictionary - Creating alias & using shallow copy 21
CODE
OUTPUT
{12: 'A', 13: 'B', 14: 'C'} {12: 'A', 13: 'B', 14: 'C'}
{12: 'A', 13: 'B', 14: 'C', 15: 'D'} {12: 'A', 13: 'B', 14: 'C'}
Dictionary - pop() 22
CODE
<Value>=<Dict>.setdefault(<key>,<default_value>)
DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K
The NEW
PURAM setdefault() returns:SCIENCE DEPT MK MD DELHI
DELHI COMPUTER OPTIONAL
PUBLIC SCHOOL R K PURAM NEW DELHI
COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT
MK MD DELHI PUBLIC SCHOOL R K PURAM NEW DELHI COMPUTER SCIENCE DEPT MK MD DELHI PUBLIC
● Value
SCHOOL of the
R K PURAM NEWkey if COMPUTER
DELHI it is in theSCIENCE
dictionary
DEPT MK MD DELHI PUBLIC SCHOOL R K PURAM NEW
OUTPUT
{10: 1, 30: 2, 50: 1, 60: 2}
Dictionary - sorted() 33
CODE