
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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'} {}
- Related Articles
- Creating curved edges with NetworkX in Python3 (Matplotlib)
- Text Analysis in Python3
- How to convert a .wav file to a spectrogram in Python3?
- How to create Python dictionary with duplicate keys?
- How to implement Multithreaded queue With Python
- How to implement jQuery ScrollLeft with easing?
- How to convert Javascript dictionary to Python dictionary?
- C++ Program to Create a Dictionary with a Dictionary Literal
- How to implement Concurrency with Threads in Python?
- How to implement MySQL CASE with OR condition?
- Python3 - Why loop doesn't work?
- How to define a Python dictionary within dictionary?
- How to create a dictionary with list comprehension in Python?
- How to implement WHILE LOOP with IF STATEMENT MySQL?
- How to implement IntFunction with lambda expression in Java?
