CBSE Class 11 Python Dictionary Comprehensive Notes with Practice Questions

Share with others

What is Python Dictionary?

A python dictionary is a collection of elements where each element is a combination of Key-Value pair. Each value/values is associated with a unique key. All the Key-Value pairs are enclosed in Curly braces. In other words we can say that Dictionaries are mutable, unordered collection of elements in the form of Key-Value Pairs which are enclosed in curly braces“.

Example of Dictionary:

A = {1 : “One”, 2 : “Two”, 3 : “Three”}

B = {“A” : “Apple”, “B” : “Ball”, “C” : “Cat”}

Dictionary A has numeric Keys (1, 2, 3)and Values(“One”, “Two”, “Three” ) are in String, while dictionary B has both Keys(‘A’, ‘B’, ‘C’) and Values(“Apple”, “Ball”, “Cat”) are in string.

Characteristics of Python Dictionary:

1. The combination of Key and Value is called Key-Value Pair.
2. Keys and it’s values are separated by colon(:)
3. Different Key-Value pairs are separated by comma(,).
4. Keys are unique for each Value.
5. Keys of dictionary must be of immutable type like string, number etc.

Method to create Empty Dictionary

There are two ways to create an empty dictionary which are as follows

1. A = { } # A is an empty dictionary
2. A = dict( ) # dict( ) method will create an empty dictionary.

Method to create Dictionary at run time :

Q1. Write a program to enter roll number and names of five students and store the data in dictionary.

```d = { }
for i in range(5):
rno=int(input("Enter roll number"))
name=input("Enter name of student")
d[rno] = name
print(d)

#Execution of Above Program

Enter roll number: 1
Enter name of student: Amit
Enter roll number: 2
Enter name of student: Sunil
Enter roll number: 3
Enter name of student: Lata
Enter roll number: 4
Enter name of student: Suman
Enter roll number: 5
Enter name of student: Ravi

{1: 'Amit', 2: 'Sunil', 3: 'Lata', 4: 'Suman', 5: 'Ravi'}```

Q2. Write a program to store book id, book name and price of three books and store the data in dictionary named “dict”

```dict = {}
for i in range(3):
b_id = int(input("Enter book id : "))
b_name = input("Enter book name : ")
b_price = int(input("Enter price of book : "))
temp=[b_name,b_price]
dict[b_id] = temp
print(dict)

#Execution of Above Program

Enter book id : 1
Enter book name : CS
Enter price of book : 350
Enter book id : 2
Enter book name : GK
Enter price of book : 100
Enter book id : 3
Enter book name : IP
Enter price of book : 250
{1: ['CS', 350], 2: ['GK', 100], 3: ['IP', 250]}```

Traversing a Python Dictionary

It means to access each element of dictionary by using loop. for example

```A = {1 : "One", 2 : "Two", 3 : "Three"}
for k in A:
print(k, "--->", A[k])

OUTPUT

1--->One
2--->Two
3--->Three```

How to append value in python Dictionary

We can easily add new element in dictionary by the following way

```A = {1 : "One", 2 : "Two", 3 : "Three"}
A[4] = "Four"
print(A)

OUTPUT

{1 : "One", 2 : "Two", 3 : "Three", 4 : "Four"}```

We can also join two dictionaries into one by using update() method. It merge the keys and values of one dictionary into other and overwrites the values of the same key.

```A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
A.update(B)
print(A)

OUTPUT

{1: 'Amit', 2: 'Sunil', 3: 'Three', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}

#It over writes the values of same keys and add the values of different keys
```

Update values in a Python Dictionary :

We can not change the key of an element, but can change the value of a respective key as follows

```B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
B[2] = 'Ram'
print(B)

OUTPUT:

{1: 'Amit', 2: 'Ram', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
```

Removing an element from a Dictionary

There are two ways by which we can delete the elements of dictionary:

1.By using del statement :

Syntax of using del statement is : del <dictionary-name>[key of element]

```B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
del B[2] # It will remove the element of key 2
print(B)

OUTPUT:

{1: 'Amit', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}

B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
del B[3]  # It will return an error (KeyError) if the key given is not present in the dictionary
print(B)

OUTPUT:
KeyError ```

2. By Using pop() function : This function not only delete the element of required key but also return the deleted value.

```B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
a=B.pop(2) #It returns the element of Key - 2
print(a)
print(B)

OUTPUT

Sunil
{1: 'Amit', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
___________________________________________________________________________________________________________________

B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
a=B.pop(6) #It returns the element of Key - 6
print(a)
print(B)

OUTPUT

Suman
{1: 'Amit', 2: 'Sunil', 5: 'Lata', 7: 'Ravi'}```