In this Python tutorial, we will learn about Python Dictionary and we will also covers different examples related to Python Dictionary. And we will also covers these topics.
- Python dictionary
- Create a dictionary in Python
- Adding element to a dictionary in Python
- Accessing element of dictionary in Python
- Removing element from a dictionary in Python
Python dictionary
In Python dictionary is used to store the values in key value pairs. The dictionary is also defines as a collection of ordered and changeable values. Dictionary are written with curly braces and separated by commas. The dictionary the value can be of any datatype and can be duplicated.
Example:
In the following example build a Python dictionary curly braces and separated by commas. Here we are written Pythontpoint in curly braces separated with commas.
Dic = {1: 'Python', 2: 'T', 3: 'Point'}
print(Dic)
Dic ={1: 'John', 2: 'Smith'}
print(Dic)
Output:
In the following output we can see that the dictionary with key value pair is printed on the screen by using print() function.

Github Link
Check this code in Repository from Github and you can also fork this code.
Github User Name: PythonT-Point
Create a dictionary in Python
A dictionary in python can be created by placed an elements in the curly brackets and separated by commas. It also carry the pair of values as a key values. The value in dictionary can be duplicate but can not be repeatable.
Examples:
In the following example we are creating a dictionary with integer and mixed keys and also creating an empty dictionary and creating a dictionary with dict() method.
- Dic = {1: ‘Python’, 2: ‘T’, 3: ‘Point’} is used to create dictionary with integer keys.
- print(“\n With the use of Integer Keys create a dictionary: “) is used to print dictionary with the use of integer.
- Dic = {‘Name’: ‘Pythontpoint’, 1: [2, 4, 6, 8]} is used to create dictionary with mixed values.
- Dic = {} is used to create an empty dictionary.
- Dic = dict({1: ‘Python’, 2: ‘T’, 3: ‘Point’}) is used to create a dictionary with the help of dict() function.
# Create a Dictionary with Integer Keys
Dic = {1: 'Python', 2: 'T', 3: 'Point'}
print("\n With the use of Integer Keys create a dictionary: ")
print(Dic)
# Create a Dictionary with mixed keys
Dic = {'Name': 'Pythontpoint', 1: [2, 4, 6, 8]}
print("\n With the use of mixed Keys create dictionary: ")
print(Dic)
# Create an empty Dictionary
Dic = {}
print("Empty Dictionary: ")
print(Dic)
# Creating a Dictionary with dict() method
Dic = dict({1: 'Python', 2: 'T', 3: 'Point'})
print("\n With the use of dict() create dictionary: ")
print(Dic)
Output:
After running the above code we get the following output in which we can see that the dictionary is created with keys and dict() function.

So, with this we have learned how to create a dictionary in python and further we will learn how to create a dictionary in python.
Read: Python List
Adding element to a dictionary in Python
We can also add the elements by different ways. The value can be added to the dictionary along with key and we can also added a nested value to a dictionary and also update a dictionary by using update() method.
Example:
In the following example we are adding the element to a dictionary in python. Firstly we are creating the empty dictionary the adding the elements to the dictionary one at a time after that add the set of values to a single key then update the existing key value after that adding the nested key value to dictionary.
# Create an empty Dictionary
Dic = {}
print("Empty Dictionary: ")
print(Dic)
# Adding elements one at a time
Dic[1] = 'Python'
Dic[2] = 'Tpoint'
print("\nDictionary after adding 2 elements: ")
print(Dic)
# Add the set of values to a single Key
Dic['setofvalues'] = 2, 4, 6
print("\nDictionary after adding 3 elements: ")
print(Dic)
# Update the existing Key's Value
Dic[1] = 'MOSTWELCOME'
print("\nUpdate the key value: ")
print(Dic)
# Adding Nested Key value to Dictionary
Dic[4] = {'Nested': {'0': 'Live', '1': 'Life'}}
print("\nAdd a Nested Key: ")
print(Dic)
Output:
After running the above code we get the following output in which we can see that the elements are added to the dictionary in python.

Read: Python Literals
Accessing element of dictionary in Python
For accessing the elements of a dictionary suggested to as its key values and the key values can be paced inside the square brackets.
Examples:
In the following example we are accessing the elements of dictionary in python by simply uses the key values and these key values are placed inside the square brackets.
# Python program to accessing an element from a Dictionary
access_dic = {121: "John", 122: "Smith", 123: "Keller"}
# prints the acccessing element
print(access_dic[121])
print(access_dic[123])
Output:
In this output, you can see that the values are accessing with the help of key value and printing on the screen.

Read: Python Strings
Removing element from a dictionary in Python
In Python we are using the del statement that remove an element from the dictionary. We are simply using del statement and the key value and those key value can be removed from the dictionary.
Example:
In the following example we are defining a dictionary and then print the original dictionary after that want to remove the element from the above dictionary by using del statement and then print the updated dictionary.
rm_studentid= {101: "Metairie", 102: "Joseph", 103: "Peggy"}
print("Initial Dictionary: ", rm_studentid)
del rm_studentid[102]
print("Updated Dictionary ", rm_studentid)
Output:
After running the above code we will get the following output in which we can see that the initial dictionary and the updated dictionary is printed on the screen.

So, in this tutorial, we have learned about the Python Dictionary and we have covered all these topics.
- Python dictionary
- Create a dictionary in Python
- Adding element to a dictionary in Python
- Accessing element of dictionary in Python
- Removing element from a dictionary in Python
Do follow the following tutorials also:

Comments are closed.