PREV NEXT
- In this tutorial we can learn about python dictionary, it is one of the most important concept in python programming language
- Dictionaries are mutable values,it means we can add,delete,update the values according to our requirement
- Values are not stored in sequence order
- Dictionary contains two values key and value ,which is separate by ‘:’
- Dictionaries created by using curly braces {}
- In Dictionary keys are always unique and vules might be can repeated
- We can traverse the dictionary by using the key using esily
Synatax:
Dictionary_Name={‘key1′:value1,’key2′:value2,’key3′:value3,’key4′:value4 ……………’keyn’:valuen}
Access The Dictionary:
Dictionary_Name[‘key1’]
Dictionary_Name[‘key2’]
………………………………….
…………………………………
………………………………….
Dictionary_Name[‘keyn’]
Note: We can create the empty dictionary also
Dictionary_Name={}
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
phone_book={ 'ramu':'1234568790', 'ramya':'9865321478', 'santhosh':'9325698789', 'ajay':'6326598752', 'vinod':'9862365981' } print ("phone number:",phone_book['ramu']) print ("phone number:",phone_book['ramya']) print ("phone number:",phone_book['santhosh']) print ("phone number:",phone_book['ajay']) print ("phone number:",phone_book['vinod']) |
Output:
1 2 3 4 5 6 7 8 9 |
phone number: 1234568790 phone number: 9865321478 phone number: 9325698789 phone number: 6326598752 phone number: 9862365981 |
- The above example is creating the dictionary of 5 elements.In each element first string is a key and second string is value i.e ramu is the key for the value of 1234567890,so rest of all also created in same manner
- We can create the dictionary by using predefined method “dict()”,by using this method we can create the dictionary.But here we can’t use use like key and pairs,we can pass the keyword arguments
Syntax:
Dictionary_Name=dict()
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
phone_book=dict( ramu='1234568790', ramya='9865321478', santhosh='9325698789', ajay='6326598752', vinod='9862365981' ) print ("phone number:",phone_book['ramu']) print ("phone number:",phone_book['ramya']) print ("phone number:",phone_book['santhosh']) print ("phone number:",phone_book['ajay']) print ("phone number:",phone_book['vinod']) |
Output:
1 2 3 4 5 6 7 8 9 |
phone number: 1234568790 phone number: 9865321478 phone number: 9325698789 phone number: 6326598752 phone number: 9862365981 |
If particular key not found in dictionary then it throughs the error is as ‘Keyerror’
Traceback (most recent call last):
File “dict.py”, line 12, in <module>
print (“phone number:”,phone_book[‘vnod’])
KeyError: ‘vnod’
We can create the dictionay combination of multiple datat types i.string,integers,floats etc
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
combine_dict={ 'fruit':'Apple', 'contact':9865485231, 'fees':52426.365, 'complex':4+1j } print ("fruit is:",combine_dict['fruit']) print ("contact is:",combine_dict['contact']) print ("fees is:",combine_dict['fees']) print ("complex is:",combine_dict['complex']) |
Output:
1 2 3 4 5 6 7 |
fruit is: Apple contact is: 9865485231 fees is: 52426.365 complex is: (4+1j) |
Add ,update and delete the elements the existing dictionary
Add:
We add the new element to the alraedy existing dictionary
Syntax:
dictionary_name[‘Nekey’]=value
Update:
We can update already exsting element to the alraedy existing dictionary
Syntax:
dictionary_name[‘existingkey’]=value
delete:
By using delete we can delete individual element from the dictionary
Syntax:
del dictionary_name[‘existingkey’]
Example:
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 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("Before modifyiing the details") print ("*********************************") print ("Name is:",student_details['name']) print ("Skill is:",student_details['skill']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("*********************************") student_details['BloodGroup']="B+" print ("After adding the new element to the dictionary") print ("*********************************") print ("Name is:",student_details['name']) print ("Skill is:",student_details['skill']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("Blood Group is:",student_details['BloodGroup']) print ("*********************************") student_details['skill']="C" print ("After update the exist element to the dictionary") print ("*********************************") print ("Name is:",student_details['name']) print ("Skill is:",student_details['skill']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("Blood Group is:",student_details['BloodGroup']) print ("*********************************") del student_details['skill'] print ("After delete the element dictionary") print ("*********************************") print ("Name is:",student_details['name']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("Blood Group is:",student_details['BloodGroup']) print ("Skill is:",student_details['skill']) print ("*********************************") |
Output:
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 |
Before modifyiing the details ********************************* Name is: samad Skill is: Python Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore ********************************* After adding the new element to the dictionary ********************************* Name is: samad Skill is: Python Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore Blood Group is: B+ ********************************* After update the exist element to the dictionary ********************************* Name is: samad Skill is: C Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore Blood Group is: B+ ********************************* After delete the element dictionary ********************************* Name is: samad Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore Blood Group is: B+ Traceback (most recent call last): File "dict2.py", line 47, in <module> print ("Skill is:",student_details['skill']) KeyError: 'skill' |
Clear:
clear is method to delete the all elements in the dictionary
Syntax:
dictionary_name.clear()
Example:
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 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("Before delete the details") print ("*********************************") print ("Name is:",student_details['name']) print ("Skill is:",student_details['skill']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("*********************************") student_details.clear() print ("After delete the details") print ("*********************************") print ("Name is:",student_details['name']) print ("Skill is:",student_details['skill']) print ("Contact is:",student_details['contact']) print ("Skill is:",student_details['earns']) print ("Address is:",student_details['Address']) print ("*********************************") |
Output:
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 |
Before delete the details ********************************* Name is: samad Skill is: Python Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore After delete the details ********************************* Traceback (most recent call last): File "dict3.py", line 19, in <module> print ("Name is:",student_details['name']) KeyError: 'name<strong>'</strong> |
Len method:
This method is used to find the length of the dictionary
Syntax:
len(dictionary_name)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("Length of dictionary:",len(student_details)) |
Output:
1 |
Length of dictionary: 5 |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } for key in student_details: print(key," is :",student_details[key]) |
Output:
1 2 3 4 5 6 7 8 9 |
skill is : Python name is : samad Address is : Bangalore earns is : 5462.325 contact is : 9542132140 |
Membership operator:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("is skill in dictionary:","skill" in student_details) print ("is earns in dictionary:","earns" in student_details) print ("is fruit not in dictionary:","fruit" not in student_details) print ("is money in dictionary:","money" in student_details) |
Output:
1 2 3 4 5 6 7 |
is skill in dictionary: True is earns in dictionary: True is fruit not in dictionary: True is money in dictionary: False |
Relation operators with dictionary
we can compare the tw dictionaries by usin relational operator
Example:
1 2 3 4 5 6 7 |
dict1={'idly':20,'dosa':50,} dict2={'dosa':50, 'idly':20} print ("Both are same:",dict1==dict2) print ("Both are same:",dict1!=dict2) |
Output:
1 2 3 |
Both are same: True Both are same: False |
Keys Method:
To extract the keys we can use keys method
Syntax:
dictionary_name.keys()
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print "Keys are:",student_details.keys() |
Output:
1 |
Keys are: ['skill', 'contact', 'earns', 'name', 'Address'] |
values Method:
To extract the values we can use keys method
Syntax:
dictionary_name.values()
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print "values are:",student_details.values() |
Output:
1 |
values are: ['Python', 9542132140L, 5462.325, 'samad', 'Bangalore'] |
Items Method:
It will return the sequence of tuples,in each tuple key and value will contain
Syntax:
dictionary_name.items()
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print "tuples are:",student_details.items() |
Output:
1 2 |
tuples are: [('skill', 'Python'), ('contact', 9542132140L), ('earns', 5462.325), ('name', 'samad'), ('Address', 'Bangalore')] |
Get Method:
Get method also one of the method to get the specific value by using key ,while get we found specific in dictionary then it will return the value or else it will return the None,But when we are using key we can mentione the default statement also
Syntax:
dictionary_name.get(key,[default])
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("value:",student_details.get('name')) print ("value at:",student_details.get('fruit')) print ("value at:",student_details.get('fruit',"Oh!!Not Found")) |
Output:
1 2 3 4 5 |
value: samad value at: None value at: Oh!!Not Found |
pop method:
pop method is used delete the particular element by using the key.
Syntax:
dictionary_name.pop(‘existingkey’)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("value:",student_details.pop('name')) print ("value:",student_details.pop('name')) |
Output:
1 2 3 4 5 6 7 8 9 |
value: samad Traceback (most recent call last): File "dict5.py", line 9, in <module> print ("value:",student_details.pop('name')) KeyError: 'name' |
Popitem method:
This method delete the random elements and return thet element
Syntax:
dictionary_name.popitem()
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } print ("value:",student_details.popitem()) |
Output:
1 |
value: ('contact', 9542132140) |
Note: It will delete and returns the random value
Copy method:
it will create the new dictionary
Syntax:
copy_dictionary_name=dictionary_name.copy()
Example:
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 |
student_details={ 'name':'samad', 'skill':'Python', 'contact':9542132140, 'earns':5462.325, 'Address':'Bangalore' } copy_student_details=student_details.copy() print ("After copied the details") print ("*********************************") print ("Name is:",copy_student_details['name']) print ("Skill is:",copy_student_details['skill']) print ("Contact is:",copy_student_details['contact']) print ("Skill is:",copy_student_details['earns']) print ("Address is:",copy_student_details['Address']) print ("*********************************") |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
After copied the details ********************************* Name is: samad Skill is: Python Contact is: 9542132140 Skill is: 5462.325 Address is: Bangalore |