Dictionary Python
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value
pair. Key value is provided in the dictionary to make it more optimized and simpler.
Note – Keys in a dictionary doesn’t allows Polymorphism.
Polymorphism :
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
we will discuss about this topic in our upcoming Articles.
Creating a Dictionary
In Python, a Dictionary can be created by placing sequence of elements within curly {} braces, separated by ‘comma’, this is the syntax mainly, dictionary holds a pair of values, one being the Key and the other corresponding pair element being its Key:value
. Values in a dictionary can be of any datatype and can be duplicated, whereas keys can’t be repeated and must be immutable.
Note –
The Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly.
output :
Dictionary with the use of Integer Keys:
{1: 'Code', 2: 'For', 3: 'Job'}
Dictionary with the use of Mixed Keys:
{'Name': 'Code', 1: [1, 2, 3, 4, 5]}
Note -
Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}.
output :
Empty Dictionary:
{}
Dictionary with the use of dict() method :
{1: 'Code', 2: 'For', 3: 'Job'}
Dictionary with each item as a pair:
{1: 'Code', 2: 'Dudle'}
Nested Dictinary :
A Dictionary within the Dictionary by using of same curly braces in the curly braces
output :
{1: 'Code', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'CodeDudle'}}
Adding elements to a Dictionary
In Python Dictionary, Addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’. Updating an existing value in a Dictionary can be done by using the built-in update()
method. Nested key values can also be added to an existing Dictionary.
Note-
While adding a value, if the key value already exists, the value gets updated otherwise a new Key with the value is added to the Dictionary.
Lets see by a code
output :
Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'Code', 2: 'For', 3: 1}
Dictionary after adding 3 elements:
{0: 'Code', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}
Updated key value:
{0: 'Code', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}
Adding a Nested Key:
{0: 'Code', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'Code'}}}
Accessing elements from a Dictionary
In order to access the items of a dictionary refer to its key name.Key can be used inside square brackets to access the elements inside the dictionary.
There is another method available for accessing the elements inside the dictionary called get().
output :
Accessing a element using get:
Job
Accessing element of a nested dictionary
In order to access the value of any key in nested dictionary, use indexing [] syntax. This helps to access the elements in nested dictionary.
output :
{1: 'Code'}
Code
For
Removing Elements from Dictionary
Using del
keyword
In Python Dictionary, deletion of a element perform by using the del
keyword. Using del keyword, specific values from a dictionary as well as whole dictionary can be deleted. Items in a Nested dictionary can also be deleted by using del keyword and providing specific nested key and particular key to be deleted from that nested Dictionary.
output :
Initial Dictionary:
{5: 'Welcome', 6: 'To', 7: 'Code', 'A': {1: 'Code', 2: 'For', 3: 'Job'}, 'B': {1: 'dudle', 2: 'Life'}}
Deleting a specific key:
{5: 'Welcome', 7: 'Code', 'A': {1: 'Code', 2: 'For', 3: 'Job'}, 'B': {1: 'dudle', 2: 'Life'}}
Deleting a key from Nested Dictionary:
{5: 'Welcome', 7: 'Code', 'A': {1: 'Code', 3: 'Job'}, 'B': {1: 'dudle', 2: 'Life'}}
Using pop()
method
pop()
method is used to return and delete the value of the key specified.
Dictionary after deletion: {'name': 'For', 3: 'Job'}
Value associated to poped key is: Code
Using popitem()
method
The popitem() returns and removes an arbitrary element (key, value) pair from the dictionary.
output :
Dictionary after deletion: {1: 'Code', 'name': 'For'}
The arbitrary pair returned is: (3, 'Dudle')
Using clear()
method
All the items from a dictionary can be deleted at once by using clear()
method.
# Creating a Dictionary
In this article i have describe about dictionary
Next Article : Array in Python Set 1
Recommended Post : Set in Python
6 Comments
I love long content like this.Looking for more such content.
ReplyDeleteThanks for your comment
DeleteNice one
ReplyDeleteThanks for your comment
DeleteStat tuned, a lot more is coming
Python-dictionary tutorial for beginners Free Learn By CodeExampler
ReplyDeleteC# for Loop Statement Learn Free for Beginners by CodeExampler website
ReplyDeleteIf you have any doubt, Please let me know.