Dictionary in Python Class 11 Notes

In this section students of class 11 and 12 having computer science or I.P. will learn about the dictionary in python programming.  These notes are prepared as per the CBSE Syllabus and covers all the topics such as – accessing items in a dictionary using keys, mutability of a dictionary (adding a new term, modifying an existing item), traversing a dictionary, built-in functions/methods – len(), dict(), keys(), values(), items(), get(), update(), del(), del, clear(),
fromkeys(), copy(), pop(), popitem(), setdefault(), max(), min(), sorted().

What is Dictionary in Python?

Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

It is an unordered collection of items where each item consists of a key and a value. It is mutable (can modify its contents) but Key must be unique and immutable.

 

Creating Dictionary

It is enclosed in curly braces { } and each item is separated from other item by a comma (,).Within each item, key and value are separated by a colon(:).

dictonary_name={key:value, key: value, ……..keyN:valueN}

Example :

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
print day

Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'} >>>

 

 

Accessing Dictionary Item

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
print day
print day[1]
print day[3]

 

Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday'}
Monday
Wednesday
>>>

 

In above code we are accessing dictionary using key.

 

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
print(day.get(1))

 

Output

Monday
>>>

 

Iterating Through A Dictionary

Following example will show how dictionary items can be accessed through loop.

 

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
for i in day:
    print(day[i])

 

Output:

Monday
Tuesday
Wednesday
>>> 

 

Updating Dictionary Elements

 

We can change the individual element of dictionary.

 

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
day[1]='Sunday'
print(day)

 

OUTPUT

{1: 'Sunday', 2: 'Tuesday', 3: 'Wednesday'}
>>> 

 

Adding New Element in dictionary:

day= {1: 'Monday', 2:'Tuesday', 3: 'Wednesday'}
day[4]='Thursday'
day[5]='Friday'
day[6]='Saturday'
day[7]='Sunday'
print day

 

Output:

{1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}
>>>

 

Deleting Dictionary Elements:

 

del, pop() and clear() statement are used to remove elements from the dictionary.

 

Using del function:

day {1:'Monday',2:'Tuesday',3:'Wednesday',4:'Thursday',5:'Friday'}
del day[1]
print day

 

Output:

{2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'}
>>>

In above code we deleted only single element.

 

Built-in Dictionary Functions

♦ len(dict) Gives the total length of the dictionary. It is equal to the number of items in the dictionary.

♦ str(dict) Return a printable string representation of a dictionary

♦ type(variable) If variable is dictionary,then it would return a dictionary type.

 

Built-in Dictionary Methods

♦ clear() Removes all elements of dictionary dict

♦ copy() Returns a shallow copy of dictionary dict

♦ items()Returns a list of dict’s (key, value) tuple pairs

♦ keys()Returns list of dictionary dict’s keys

♦ setdefault(key,default=None) Similar to get (), but will set dict [key] = default if key is not already in dict

♦ update(dict2)Adds dictionary dict2’s key-values pairs to dict

♦ values() Returns list of dictionary dict’s values

 

 

Important points to remember

1. Dictionary is a collection of elements which is unordered, changeable and indexed.

2. Dictionary has keys and values.

3. Doesn’t have index for values. Keys work as indexes.

4. Dictionary doesn’t have duplicate member means no duplicate key.

5. Dictionaries are enclosed by curly braces { }

6. The key-value pairs are separated by commas ( , )

7. A dictionary key can be almost any Python type, but are usually numbers or strings.

8. Values can be assigned and accessed using square brackets [ ].

9. Keys of a dictionary must be of immutable types, such as string, number, tuple.

10. A dictionary operation that takes a key and finds the corresponding value, is called lookup.

11. There are two methods to delete elements from a dictionary: (i) using del statement (ii) using pop( ) method

12. To check the existence of a key in dictionary, two operators are used (i) in (ii) not in

 

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved