-
Notifications
You must be signed in to change notification settings - Fork 1
/
11-Dictionary.py
63 lines (50 loc) · 2.03 KB
/
11-Dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'''
author : Jaydatt
Dictionary
clear () : To remove all the elements of a dictionary
copy () : To return a copy of the dictionary
get () : To return the value of a specific key
keys () : To get a list of the keys included in a dictionary
update () : To update the dictionary by adding key:value pairs
values () : To get a list of values present in the dictionary
pop () : To remove the element of the specified key
'''
dic = {
"Name": "Rahul",
"Gender": "Male",
"Contacts": {"Mobile": 5646845 , "Email" : "xyz@abc"},
"Address": {"Country":"India","State":"Mumbai"},
1: 2}
dic["Age"] = 21 # add or modify value of key
dic['Marks'] = "85%" # add or modify value of key
del dic['Marks'] # delete Marks key and its value
del dic ['Address']['State'] # delete State key and its value
print("----------")
for key in dic : ## or for key in list(dic.keys()) :
print( key,": ", dic[key])
print("----------")
# The difference between .get(key) and [key] sytax in Dictionaries?
# if key found then returns value of key, otherwise return None
print("dic.get('Name') : ", dic.get('Name'))
# if key found then returns value of key, otherwise throws error
print("dic['Name'] : ", dic['Name'])
print("dic['Age'] : ",dic['Age'])
print("dic['Contacts']['Email'] : ", dic['Contacts']['Email'])
print("----------list(dic.keys()) : \n", list(dic.keys())) # Prints the keys of the dictionary
print("----------list(dic.values()) : \n", list(dic.values())) # Prints the values of the dictionary
print("----------list(dic.items()) : \n", list(dic.items())) # Prints the (key, value) for all contents of the dictionary
print("----------dic: \n",dic)
dic_2 = {
"Lovish": "Friend",
"Divya": "Friend",
"Shubham": "Friend",
"rahul": "A Dancer"}
# Updates the dictionary by adding key-value pairs from updateDict
dic.update(dic_2)
print("----------dic: \n",dic)
'''
we can not take list in set and error generate for example:
s = {8,7,"Amit",[5,6]}
we can take tupel in set but can not modify tupel for example:
s = {8,7,"Amit",(5,6)}
'''