How to implement Dictionary with Python3


Dictionaries in python are a type of data structure that map keys to values as a key-value pair. They are one of the frequently used data structures and have many interesting properties. They are presented by enclosing them in a pair of curly brace like below.

dict = {'day1':'Mon' ,'day2':'Tue','day3':'Wed'}

The elements or key-value pairs in the dictionary are represented within single quotes and separated by a colon.

Creating a Dictionary

We create a dictionary by assigning values written in the form of a key.

Example

Dict1 = {'day1':'Mon' ,'day2':'Tue','day3':'Wed'}
print(type(dict1))
print(dict1)

# Using the dict() method
dict2 =dict({('day1','Mon'),('day2','Tue'),('day3','Wed')})
print(type(dict2))
print(dict2)

Output

Running the above code gives us the following result −

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}

Nested Dictionaries

Dictionaries can also be nested. Means, we have a dictionary as a value for one of the keys inside another dictionary. In the below example we have Saturday and Sunday marked as elements of an inner dictionary which is nested inside an outer dictionary.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed','weekend':{'d1':'Saturday','d2':'Sunday'}}
print(dict)

Output

Running the above code gives us the following result:

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed', 'weekend': {'d1': 'Saturday', 'd2': 'Sunday'}}

Accessing Values in Dictionary

To access the elements of a dictionary, we can use the square brackets along with the key to obtain its value. We can also use the get() method to get the values for the dictionary elements.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed','weekend':{'d1':'Saturday','d2':'Sunday'}}
print(dict['day2'])
print(dict['weekend'])
print(dict.get('day3'))

Output

Running the above code gives us the following result:

Tue
{'d1': 'Saturday', 'd2': 'Sunday'}
Wed

Adding elements to a Dictionary

We add new elements to a dictionary by adding a new key value pair. We can also add another dictionary as an element to create a nested dictionary.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
dict['day4']='Thu'
dict['day5']='Fri'
print(dict)

Output

Running the above code gives us the following result:

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed', 'day4': 'Thu', 'day5': 'Fri'}

Updating Dictionary

We can update a dictionary by adding a new entry or a key-value pair and modifying an existing entry. We have already seen the addition of new elements to the dictionary above. Now we will see the modification of existing entry.Here we simply take the key and assign the new value to the element.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
dict['day1']='Monday'
dict['day2']='Tuesday'
print(dict)

Output

Running the above code gives us the following result:

{'day1': 'Monday', 'day2': 'Tuesday', 'day3': 'Wed'}

Delete Elements of a Dictionary

The specific elements of a dictionary can be deleted by using the del keyword. It can also be used to delete the entire dictionary. There is also the clear() method which can be used to remove elements from the entire dictionary.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
print(dict)
del dict['day3']
print(dict)
dict.clear()
print(dict)

Output

Running the above code gives us the following result:

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
{'day1': 'Mon', 'day2': 'Tue'}
{}

Updated on: 17-Oct-2019

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements