Professional Documents
Culture Documents
Dictionary
A dictionary in Python is a collection of items accessed by a specific key rather than by index.
Imagine a dictionary in the real world... when you need to look up the meaning of a word, you try to find the
meaning using the word itself and not the possible index of the word.
Python dictionaries work with the same concept.
Syntax
{key:values , key:values , key:values}
In [1]:
d = {}
type(d)
Out[1]:
dict
In [2]:
d = dict()
type(d)
Out[2]:
dict
Adding elements
In [5]:
square = {1:1,2:4,3:9,4:16,5:25}
In [6]:
Out[6]:
In [7]:
square[10]=100
square
Out[7]:
In [17]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
date = '22-6-2020'
# date day
month[int(date.split('-')[1])]
Out[17]:
'Jun'
Accessing a value
Python dictionary is unordered. So to get a value from it, you need to put its key
in square brackets.
In [18]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
month[6]
Out[18]:
'Jun'
In [19]:
month[2]
Out[19]:
'Feb'
In [20]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
month[3]='March'
month
Out[20]:
{1: 'Jan',
2: 'Feb',
3: 'March',
4: 'Apr',
5: 'May',
6: 'Jun',
7: 'July',
8: 'Aug',
9: 'Sep'}
In [21]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
month[10]='Oct'
month
Out[21]:
{1: 'Jan',
2: 'Feb',
3: 'Mar',
4: 'Apr',
5: 'May',
6: 'Jun',
7: 'July',
8: 'Aug',
9: 'Sep',
10: 'Oct'}
e.g.
del dict1[1]
e.g.
del dict
In [22]:
In [23]:
---------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-23-dbaf89294259> in <module>
2 dict1={1:1,2:4,3:9,4:16,5:25,6:36}
3 del dict1
----> 4 print(dict1)
In [24]:
In [25]:
In [26]:
# Example
dict = {1:'abc','name':'ram',(12,34):45}
dict
Out[26]:
In [27]:
dict = {'name':'ram',[12,34]:56}
dict
---------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-32cdf011fa55> in <module>
1 # Example of mutable key . It gives us error
2
----> 3 dict = {'name':'ram',[12,34]:56}
4 dict
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
len(month)
Out[28]:
In [31]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
max(month)
Out[31]:
In [32]:
month = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'July',8:'Aug',9:'Sep'}
min(month)
Out[32]:
In [33]:
sum(month)
Out[33]:
45
In [1]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
print("Before clear",dict1)
dict1.clear()
print("After Clear",dict1)
Before clear {'Name': 'Ram', 'Age': 34, 'City': 'Ghaziabad', 'Contact': [971
1001100, 9911001100]}
After Clear {}
2. dict.copy()
Returns a shallow copy of dictionary dict.
In [2]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
dict2=dict1.copy()
print(dict2)
3. dict.fromkeys()
Create a new dictionary with keys from seq and values set to value.
In [10]:
key=["Name","Age","City"]
value='Empty'
d = dict.fromkeys(key,value)
d
Out[10]:
4. dict.get(key1, default=None)
For key key1, returns value or default if key not in dictionary.
In [12]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
dict1['Marks']
---------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-12-d191728ff7ac> in <module>
1 dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[971100110
0,9911001100]}
----> 2 dict1['Marks']
KeyError: 'Marks'
In [16]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
print(dict1.get('Age','Keys Not available'))
34
5. dict.items()
Returns a list of dict's (key, value) tuple pairs.
In [17]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
print(dict1.items())
6. dict.keys()
Returns list of dictionary dict's keys.
In [18]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
print(dict1.keys())
7. dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict.
In [21]:
dict1={1:1,2:4,3:9}
dict2 = {1:'ram',4:16}
dict1.update(dict2)
dict1
Out[21]:
8. dict.values()
Returns list of dictionary dict's values.
In [19]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
print(dict1.values())
In [32]:
Ram
34
Ghaziabad
[9711001100, 9911001100]
In [35]:
dict1={"Name":"Ram","Age":34,"City":"Ghaziabad","Contact":[9711001100,9911001100]}
for i in dict1.items():
print(i[1])
Ram
34
Ghaziabad
[9711001100, 9911001100]
In [36]:
Out[36]:
In [43]:
else:
print("Not a valid user")
len(dict)
Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.