Dictionary Data Type in Python
Dictionary
Dictionary is the collection of the key and values. Dictionaries in Python are similar to hash tables in other languages.
Pairs in a dictionary are separated by comma(,) and individual key-value pairs are separated by colon(:) and dictionary items are delimited by curly braces { }.Key is at LHS of colon and Value is at RHS of colon.
Dictionaries are mutable objects.It is iterable objects and Dictionary elements are not subscriptable.
Dictionary Visualization
As we discussed that the dictionary does not support the index value so that is why we have to use key to print accessing any value and slicing of data is now allowed in Dictionary due to lack of index.
Create empty dictionary:
Python 3.8.4 (tags/v3.8.2:7b3ccb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = {} >>> print(data) {} >>> print(type(data))>>> print(len(data)) 0
Create non-empty dictionary:
Python 3.8.4 (tags/v3.8.2:7b3ccb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = {'roll':101,'name':'ankita','perc':95.6,'sub':('P','C','M'),'marks':[96,91,93]} >>> print(data) {'roll':101,'name':'ankita','perc':95.6,'sub':('P','C','M'),'marks':[96,91,93]} >>> print(type(data))>>> print(len(data)) 5
Accessing the Value
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> info={"Name":"Archana","Address":"Pune","Age":30} >>> info["Name"] 'Archana' >>> info["Address"] 'Pune' >>>info["Age"] 30 >>> info[2:] Traceback (most recent call last): File "", line 1, in TypeError: unhashable type: 'slice' >>>
Data Insertion
In the heading will be learning about data insertion in the Dictionary,
Syntax
dictionary_name[New_key]=Value
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> info={"Name":"Archana","Address":"Pune","Age":30} >>> info["Mobile"]=87654322344 >>> print(info) {'Name': 'Archana', 'Address': 'Pune', 'Age': 30, 'Mobile': 87654322344} >>>info["Post"]="Trainer" >>>print(info) {'Name': 'Archana', 'Address': 'Pune', 'Age': 30, 'Mobile': 87654322344,'Post':'Trainer'}
Data Deletion
Deletion is used to remove any element from the Dictionary, here python provides some in-built function to delete the value from the Dictionary. Before working with the program now will we see the in-bluit function.
Dictionary Deletion Method
Function | Description |
del dict_name | It is delete all the dict along with the element |
clear() | It deletes all the elements but lists exist. |
popitem() | Delete the value from the last |
pop() | Delete value by the key |
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more >>> print(info) {'Name': 'Archana', 'Address': 'Pune', 'Age': 30, 'Mobile': 87654322344} >>> info.pop("Address") 'Pune' >>> print(info) {'Name': 'Archana', 'Age': 30, 'Mobile': 87654322344} >>> info.popitem() ('Mobile', 87654322344) >>> info.clear() >>> print(info) {} >>> del info >>> print(info) Traceback (most recent call last): File "", line 1, in NameError: name 'info' is not defined >>>
Data Updation
Now we will see the data updation in Dictionary ,now let’s take the example,
Syntax
dict[old_key]=New_Value
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> info={"Name":"Archana","Address":"Pune","Age":30} >>> info[ ... "Name"] 'Archana' >>> info["Name"]="Rahul" >>> print(info) {'Name': 'Rahul', 'Address': 'Pune', 'Age': 30} >>> info["Address"]="New_Delhi" >>> print(info) {'Name': 'Rahul', 'Address': 'New_Delhi', 'Age': 30} >>>
Additional Feature
Function | Description |
keys() | A set-like object providing a view on Dictionary’s keys |
values() | A set-like object providing a view on Dictionary’s values |
items() | A set-like object providing a view on Dictionary’s items |
get(key, default=None) | Return the key value if it is set by default. |
setdefault(key,default=None) | Value of the key will return if the key is in the dictionary, else default. |
Example
Python 3.8.4 (tags/v3.8.2:7b3ccb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = {'roll':101,'name':'ankita','perc':95.6} >>> print(data.keys()) dict_keys(['roll', 'name', 'perc']) >>> print(data.values()) dict_values([101, 'ankita', 95.6]) >>> print(data.items()) dict_items([('roll', 101), ('name', 'ankita'), ('perc', 95.6)]) >>> data.update({'name':'Preeti','sub':('P','C','M')}) >>> print(data) {'roll': 101, 'name': 'Preeti', 'perc': 95.6, 'sub': ('P', 'C', 'M')} >>> print(data.get('name')) Preeti >>> print(data.get('age')) None >>> print(data.get('age',0)) 0 >>> print(data.setdefault('name')) Preeti >>> print(data.setdefault('age',0)) 0 >>> print(data) {'roll': 101, 'name': 'Preeti', 'perc': 95.6, 'sub': ('P', 'C', 'M'), 'age': 0}
Dictionary Operations:
Operator | Description |
= |
|
in not in | Used to test membership of a key in a given dictionary. Return True/False. |
is is not | Used to test the identity of two dictionary objects. |
Example
Python 3.8.4 (tags/v3.8.2:7b3ccb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32 >>> data = {'roll':101,'name':'ankita','perc':95.6,'sub':('P','C','M'),'marks':[96,91,93]} >>> data['roll'] = 102 >>> print(data) {'roll':102,'name':'ankita','perc':95.6,'sub':('P','C','M'),'marks':[96,91,93]} >>> print('sub' in data) True >>> print('age' not in data) True >>> a = {'roll': 101,'name':'ankita'} >>> b = {'roll': 101,'name':'ankita'} >>> print(a is b) False >>> print(a is not b) True