Review of Python-Dictionary (1)
Review of Python-Dictionary (1)
Introduction
Dictionary is an unordered collection (no indexing) of items where each item consist of a
key and a value, ie.,key-value pair.
If we want to group multiple elements under a single variable, these datatypes or
collections are used in Python.
Each element in a dictionary can be accessed by a unique key.
It is mutable(can modify its values), but key must be unique and immutable.
dict={‘R’:’Rainy’,’S’:’Summer’,’W’:’Winter’,’A’:’Autumn’}
Keys
print(dict)
Output is {'R': 'Rainy', 'S': 'Summer', 'W': 'Winter', 'A': 'Autumn’}
dict={‘R’:’Rainy’,’S’:’Summer’,’W’:’Winter’,’A’:’Autumn’,’W’:’Winter Season’}
print(dict)
Output is {'R': 'Rainy', 'S': 'Summer', 'W': 'Winter Season', 'A': 'Autumn'}
Methods to create a Dictionary
1.To create an empty dictionary, put two curly braces:
Example: >>>d={}
>>>type(d)
<class ‘dict’>
2.Enclose key-value pair in curly braces:
Example: >>>d={‘Name’:’Amy’,’Age’:16,’Class’:11}
>>>print(d)
{‘Name’:’Amy’,’Age’:16,’Class’:12}
3.Using dict():
Add an item to a dictionary
To add an item to a dictionary, we can use square bracket([])
for accessing and initializing dictionary values.
Example: >>>d={}
>>>d[1]=“One”
>>>d[2]=“Two”
>>>d[3]=“Three”
>>>print(d)
{1:”One”,2:”Two”,3:”Three”}
Accessing Elements in Dictionary
To access dictionary elements, we can use square brackets along with the key to obtain its
value.
Example:
>>>d={‘Name’:’Riya’,’Age’:7,’Class’:’Second’}
>>>print(d[‘Name’] or print(“d[‘Name’]:“,d[‘Name’]) or print(d.get(‘Name’))
Riya
>>>print(d[‘Age’] or print(“d[‘Age’]:”,d[‘Age’]) or print(d.get(‘Age’))
7
If we try to access element with a key which is not a part of dictionary, we get an error as
follows:
>>>d={‘Name’:’Riya’,’Age’:7,’Class’:’Second’}
>>>print(d[‘Aman’]
Traceback (most recent call last)
File”<pyshell#16>”,line 1,in <module>
print(d[‘Aman’]
Key error:’Aman’
Traversing a Dictionary
Traversing a dictionary means accessing each element in a dictionary.
This can be done through for loops.
Example 1:
d={1:’One’,2:’Two’,3:’Three’} Output 1 One
for i in d: # i takes the key not values. 2 Two
print(i,d[i]) 3 Three
Example 2:
d={1:’One’,2:’Two’,3:’Three’} Output 1:One
for i in d: # i takes the key not values. 2:Two
print(i,”:”,d[i]) 3:Three
Appending Values in a dictionary
We can add elements to an existing dictionary.
Example:
D={‘Sun’:’Sunday’,’Mon’:’Monday’}
D[‘Tue’]=‘Tuesday’
print(D)
Output
{‘Sun’:’Sunday’,’Mon’:’Monday’,’Tue’:’Tuesday’}
Updating Dictionary Elements
We can modify the individual elements of a dictionary.
Example:
d={‘Teena’:18,’Riya’:12,’Ameya’:22,’Ram’:25}
d[‘Riya’]=28
print(d) Output {‘Teena’:18,’Riya’:28,’Ameya’:22,’Ram’:25}
Two dictionaries can be merged into one by using update() method.
Example:
d1={1:10,2:20,3:30}
d2={4:40,5:50}
print(d1.update(d2)) Output {1:10,2:20,3:30,4:40,5:50}
Example:
d1=={1:10,2:30,3:30,5:40,6:60}
d2={1:10,2:20,3:30}
print(d1.update(d2)) Output {1:10,2:203:30,5:40,6:60}
Removing an item from Dictionary
Using del command:
Example:
A={‘Mon’:’Monday’,’Tue’:’Tuesday’,’Wed’:’Wednesday’,’Thur’:’Thursday’}
print(del A[‘Wed’]) Output {‘Mon’:’Monday’,’Tue’:’Tuesday’,’Thur’:’Thursday’}
print(del A[‘Fri’]) KeyError:’Fri’
keys():
It returns a list of key values in the dictionary.
Example:
>>>d={‘Sun’:’Sunday’,’Mon’:’Monday’,’Tue’:’Tuesday’,’Wed’:’Wednesday’}
>>>d.keys()
dict_items([‘Sun’,’Mon’,’Tue’,’Wed’])
values():
It returns a list of values from key-value pairs in a dictionary.
Example:
>>>d={‘Sun’:’Sunday’,’Mon’:’Monday’,’Tue’:’Tuesday’,’Wed’:’Wednesday’}
>>>d.values()
dict_items([‘Sunday’,’Monday’,’Tuesday’,’Wednesday’])