Dictionaries in Python Class 11 Notes

Share with others

Teachers and Examiners (CBSESkillEduction) collaborated to create the Dictionaries in Python Class 11 Notes. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

Dictionaries in Python Class 11 Notes

Introduction to Dictionaries

A dictionary is a data structure that stores information in key-value pairs. In dictionary keys and values are separated by a colon (:) and different key-value pairs are separated by commas.

Creating a Dictionary

In Python, dictionaries are created using curly braces {}. Each item is a pair of key values that are separated by colons. Each key is linked to a value; keys and values are separated by a colon. Different key-value pairs are separated by commas.

Example –

dict3 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
print(dict3)

Output:
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
Accessing Items in a Dictionary

We have already seen how an approach known as indexing is used to retrieve the elements of a sequence (string, list, and tuple). The keys in a dictionary are used to access the objects rather than their indices or relative places. Each key corresponds to a value and acts as an index.

Example –

dict3 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict3['Ram'])
print(dict3['Sangeeta'])

Output:
89
85
Dictionaries are Mutable

Dictionaries are mutable which implies that the contents of the dictionary can be changed after it has been created.

Adding a new item

We can add a new item to the dictionary as shown in the following example –

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
dict1['Meena'] = 78
print(dict1)

Output:
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Meena': 78}
Modifying an Existing Item

The existing dictionary can be modified by just overwriting the key-value pair. Example to modify a given item in the dictionary –

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
dict1['Suhel'] = 93.5
print(dict1)

Output:
{'Mohan': 95, 'Ram': 89, 'Suhel': 93.5, 'Sangeeta': 85}
Dictionary Operations
Membership

The membership operator in checks if the key is present in the dictionary and returns True, else it returns False.

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
print('Suhel' in dict1)

Output:
True

The not in operator returns True if the key is not present in the dictionary, else it returns False.

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
print('Suhel' not in dict1)

Output:
False
Traversing a Dictionary

We can access each item of the dictionary or traverse a dictionary using for loop.

Example –

dict1 ={'Mohan':95,'Ram':89,'Suhel':92,'Sangeeta':85}
for key in dict1:
    print(key,':',dict1[key])

Output:
Mohan : 95
Ram : 89
Suhel : 92
Sangeeta : 85

Dictionary methods and Built-in functions

Python provides many functions to work on dictionaries.

len()

Returns the length or number of key: value pairs of the dictionary passed as the argument.

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
print(len(dict1))

Output:
4
dict()

Creates a dictionary from a sequence of key-value pairs.

Example –

pair1 = [('Mohan',95),('Ram',89),('Suhel',92),('Sangeeta',85)]
dict1 = dict(pair1)
print(dict1)

Output:
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85}
keys()

Returns a list of keys in the dictionary.

Example –

dict1 = {'Mohan':95, 'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict1.keys())

Output:
dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])
values()

Returns a list of values in the dictionary.

Example –

dict1 = {'Mohan':95, 'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict1.values())

Output:
dict_values([95, 89, 92, 85])
items()

Returns a list of tuples(key – value) pair

Example –

dict1 = {'Mohan':95, 'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict1.items())

Output:
dict_items([('Mohan', 95), ('Ram', 89), ('Suhel', 92), ('Sangeeta', 85)])
get()

Returns the value corresponding to the key passed as the argument.

Example –

dict1 = {'Mohan':95, 'Ram':89,'Suhel':92, 'Sangeeta':85}
print(dict1.get('Sangeeta'))

Output:
85
update()

appends the key-value pair of the dictionary passed as the argument to the key-value pair of the given dictionary.

Example –

dict1 = {'Mohan':95, 'Ram':89,'Suhel':92, 'Sangeeta':85}
dict2 = {'Sohan':79,'Geeta':89}
dict1.update(dict2)
print(dict1)
print(dict2)

Output:
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta': 85, 'Sohan': 79, 'Geeta': 89}
{'Sohan': 79, 'Geeta': 89}
del()

Deletes the item with the given key To delete the dictionary from the memory we write: del Dict_name

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
del dict1['Ram']
print(dict1)

Output:
{'Mohan': 95, 'Suhel': 92, 'Sangeeta': 85}
clear()

Deletes or clear all the items of the dictionary

Example –

dict1 = {'Mohan':95,'Ram':89,'Suhel':92, 'Sangeeta':85}
dict1.clear()
print(dict1)

Output:
{}
Computer Science Class 11 Notes
Computer Science Class 11 MCQ
Computer Science Class 11 NCERT Solutions

Computer


Share with others

Leave a Comment