-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictionaries.py
executable file
·149 lines (127 loc) · 3.7 KB
/
dictionaries.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# Python Dictionaries
#
# Dictionary
#
# A dictionary is a collection which is unordered, changeable and indexed.
# In Python dictionaries are written with curly brackets, and the have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print (thisdict)
# Accessing Items
#
# You can access the items of a dictionary by referring to its key name:
x = thisdict["model"]
x = thisdict.get("model")
# Change Values
#
# You can change the value of a specific item by referring to its key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
# Loop Through a Dictionary
#
# Print all key names in the dictionary, one by one:
for x in thisdict:
print(x)
# Print all values in the dictionary, one by one:
for x in thisdict:
print(thisdict[x])
for x in thisdict.values():
print(x)
# Loop through both keys and values, by using the items() function:
for x, y in thisdict.items():
print(x, y)
# Dictionary Length
#
# To determine how many items (key-value pairs) a dictionary have, use the len method.
print( len(thisdict) )
# Adding Items
#
# Adding an item to the dictionary is done by using a new index key and
# assign a value to it:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
# Removing Items
#
# There are several methods to remove items from a dictionary:
# The del keyword removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
# The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
#The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
# The del keyword removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
# The del keyword can also delete the dictionary completely:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
#print(thisdict) # this will cause an error because "thislist" no longer exists.
# The clear() keyword empties the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
# The dict() Constructor
#
# It is also possible to use the dict() constructor to make a dictionary:
thisdict = dict(brand="Ford", model="Mustang", year=1964)
print(thisdict)
# Dictionary Methods
#
# Python has a set of built-in methods that you can use on dictionaries.
#
# Method Description
# clear() Removes all the elements from the dictionary
# copy() Returns a copy of the dictionary
# fromkeys() Returns a dictionary with the specified keys and values
# get() Returns the value of the specified key
# items() Returns a list containing the a tuple for each key value pair
# keys() Returns a list containing the dictionary's keys
# pop() Removes the element with the specified key
# popitem() Removes the last inserted key-value pair
# setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
# update() Updates the dictionary with the specified key-value pairs
# values() Returns a list of all the values in the dictionary