-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path5_dictionaries.py
More file actions
68 lines (43 loc) · 1.02 KB
/
5_dictionaries.py
File metadata and controls
68 lines (43 loc) · 1.02 KB
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
64
65
66
67
68
'''
Definition
A dictionary is a collection of key/value pairs, very similar to lists except you use keys instead of indexes to access the values in it.
Syntax:
d = { key1: val1, key2: val2, ... }
A key can be of any immutable data type.
'''
#d = { [1,2] : True }
#print(d)
d = { (1,2) : True }
print(d)
emails = {} or dict()
emails = { "David" : "david.r@abc.com" }
#Accessing
emails = { "David" : "david.r@abc.com" }
print (emails["David"])
print (emails["Sam"])
print (emails.get("Sam", None))
#Add
emails["Brian"] = "brian123@testmail.com"
emails["Tom"] = "tom.sawyer@xyz.com"
#Modify
emails["David"] = "david.s@abc.com"
#Remove
del emails["David"]
#update
new_emails = { "David" : "david.r@xyz.com", "Sam" : "sam.wes@testmail.com" }
emails.update(new_emails)
#pop, popitem
element = emails.pop("Sam")
print(element)
emails["Sam"] = "sam.wes@testmail.com"
element = emails.popitem()
print(element)
#items, keys and values
emails.items()
emails.keys()
emails.values()
#len
len(email)
#clear
emails.clear()
print(emails)