Dictionary

Dictionary:

mutable, dictionary can be modified.
Dictionary is a collection of key value pairs, for example: {β€˜a’:1,’b’:2,’c’:3}
Dictionary keys and values are sequences, iterables
1
d={'a':1,'b':2,'c':3}
2
for i in d.keys():
3
print(i)
4
5
a
6
b
7
c
8
​
9
d={'a':1,'b':2,'c':3}
10
for i in d.values():
11
print(i)
12
13
1
14
2
15
3
Copied!
d[β€˜a’] is 1, d[β€˜b’] is 2, d[β€˜c’] is 3
You can also create a dictionary that way, use function dict()
d=dict(a=1,b=2,c=3)
Above is the same as
d={β€˜a’:1,’b’:2,’c’:3}
d['a'] is 1
Dictionary is an object, meaning it has methods and attributes that can be invoked
To see all methods, type
help(<dictionary variable>)
In Jupyter notebook, to see list of methods or attributes
Press shift key after enter <Dictionary variable>.
​
Last modified 1yr ago
Copy link