Python Dictionary With Illustrations – Detailed FAQ

Python has a mutable data type called dictionary. A Python dictionary is a list of key and value pairs that are surrounded in curly braces and separated by colons (:).

Dictionary for Python

We have a dictionary right here. The key is on the colon’s left side, while the value is on the colon’s right side.

mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}

Important Points:
1. In a dictionary, keys must be distinct; duplicate values are permitted.
2. If a dictionary doesn’t include any key-value pairs, it is said to be empty. The symbol for an empty dictionary is.
3. Dictionary keys must be made up of immutable data types like Strings, integers, or tuples.

Python keys for dictionary value access

As illustrated in the following example, we can access a value by using the relevant key in the square brackets. Dictionary name in square brackets after the key for which the value is desired is specified.

mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuAge'])
print("Student City is:", mydict['StuCity'])

Python dictionary example

A compilation error will be returned if you give a key that doesn’t exist in the dictionary. An illustration. When we run this code, a compilation error occurs because we are attempting to get a value for the key ‘StuClass’ that does not exist in the dictionary mydict.

mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuClass'])
print("Student City is:", mydict['StuCity'])

Python dictionary error

Alter dictionary values
The values for the current key-value pairs are being updated here. We use the appropriate key in a dictionary to change a value.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age before update is:", mydict['StuAge'])
print("Student City before update is:", mydict['StuCity'])
mydict['StuAge'] = 31
mydict['StuCity'] = 'Noida'
print("Student Age after update is:", mydict['StuAge'])
print("Student City after update is:", mydict['StuCity'])

Python dictionary update values

Adding a new dictionary entry (key-value pair)

A new key-value pair can be added to an existing dictionary. To further grasp this, let’s use an illustration.

mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
mydict['StuClass'] = 'Jr.KG'
print("Student Name is:", mydict['StuName'])
print("Student Class is:", mydict['StuClass'])

Python adding new key value pair in dictionary

iterate over a dictionary

The example that follows demonstrates how to loop through a dictionary. For loop is being used here.

mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
for e in mydict:

Python loop through dictionary

Python dictionary deletion action

In Python, we may remove both key-value pairs and whole dictionaries. Take this as an example. As you can see, we can specify the key to remove the desired key value pair from the dictionary by using del after the dictionary name and in square brackets.

We can use the clear() method to remove all of the entries (all key-value pairs) from the dictionary.

Use the del keyword followed by the name of the dictionary to remove the entire dictionary and all of its data, as demonstrated in the example below.

mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
del mydict['StuCity']; # remove entry with key 'StuCity'
mydict.clear();     # remove all key-value pairs from mydict
del mydict ;        # delete entire dictionary mydict

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button