0% found this document useful (0 votes)
17 views5 pages

Chapter 7 Dictionaries 1

Uploaded by

vimleshkumari341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Chapter 7 Dictionaries 1

Uploaded by

vimleshkumari341
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter-7 Dictionaries

Dictionary is an unordered collection of elements where each element is a key : value pair, separated by
commas enclosed inside curly brackets. Dictionary is a mutable datatype.
Characteristic features of a dictionary:-
(i) Dictionary is unordered collection of elements
(ii) Each element of a dictionary is key : value pair.
(iii) Each key is separated from it’s value by a colon ( : )
(iv) Keys of a dictionary must be of immutable datatype (If you try to create a dictionary with keys of
mutable datatype, it gives TypeError : unhasable type : ‘list’
(v) Dictionary is a mutable datatype i.e. it’s value can be manipulated inplace.

Creation of dictionary:-
Syntax:-
<DictionaryName> = { key : value , [ . . . ] }
Example :-
std = { “roll” : 1 , “name” : “amit” }
Creation of empty dictionary:-
METHOD-1
Syntax:-
<DictionaryName> = { }
Example:-
std = { }
METHOD-2
Syntax:-
<DictionaryName> = dict ( )
Example:-
std = dict ( )
Adding an element/item in a dictionary:-
Syntax:-
<DictionaryName> [ key ] = item/element
Example:-
std [“name”] = “Anjali”
Creating a dictionary as a copy of an existing dictionary:-
Syntax:-
<DictionaryName> = <ExistingDictionaryName>
Example:-
std = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
newstd = std # creating a copy of dictionary std into newstd
Relational operators used with Dictionary:-
Only two relational operator ( == and != ) can be used to compare two dictionaries.
( NOTE:- No other relational operator like > , < , >= and <= can be used with dictionary object.)
Example:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
D2 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
print ( D1 = = D2 ) # True
print ( D1 != D2 ) # False
Membership operators (in / not in) with Dictionary:-
The in operator checks whether a particular key value is present in the dictionary or not. It returns True if
the key appears in the dictionary, otherwise returns False. The not in operator returns True if key does not
appears in the dictionary.
Example-1:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
print ( 2 in D1) # True
Example-2:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
print ( 2 not in D1) # False
Accessing dictionary elements:-
For accessing dictionary element, dictionary name with corresponding key enclosed inside square brackets is
used.
Example:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
print (D1[2]) # Sristhi
Traversing dictionary elements:-
Traversing operation means accessing each element/item of an object (list/dictionary/tuple/string)
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
for i in D1:
print (D1[i])
Output:-
Anjali
Sristhi
Mehak
Dictionary methods/functions-
1) len( ):- This function returns the number of elements (key-value pairs) in the dictionary.
Syntax:-
len (dictionary_name)
Example:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
print ( len (D1) ) # 3
2) clear( ):- This function removes all the elements from the dictionary.
Syntax:-
dictionary_name.clear ( )
Example:-
D1 = { 1 : “Anjali” , 2 : “Sristhi” , 3 : “Mehak” }
D1.clear ( ) # { }
3) get( ):- This function returns a value for the given key. If key is not present in the dictionary, then reurns
None.
Syntax:-
Python_Object = dictionary_name.get (key)
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
a=D1.get(2)
print(a) # Sristhi
4) keys( ):- This function returns all the keys of a dictionary.
Syntax:-
dictionary_name.keys ( )
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
print( D1.keys ( )) # dict_keys([1, 2, 3])
5) values( ):- This function returns all the values of a dictionary.
Syntax:-
dictionary_name.values ( )
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
print( D1.values ( )) # dict_values(['Anjali', 'Sristhi', 'Mehak'])
6) items( ):- This function returns all the items in the key-value pair form.
Syntax:-
dictionary_name.items ( )
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
print( D1.items ( )) # dict_items([(1, 'Anjali'), (2, 'Sristhi'), (3, 'Mehak')])
7) update( ):- This function merges key:value pairs from new dictionary into the original dictionary by
adding or replacing the values needed. If items are not present in the old dictionary, they are added in that
and if the key of the item is already present in the old dictionary, their value is updated with the new value.
Syntax:-
Old_dictionary_name.update (new_dictionary_name )
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
D2 = { 4: "Rajesh", 1: "Ramesh"}
D1.update(D2)
print(D1) # {1: 'Ramesh', 2: 'Sristhi', 3: 'Mehak', 4: 'Rajesh'}
8) pop ( ):- This function removes/deletes an element from the dictionary using it's key and also returns the
deleted element. If key is not found it returns KeyError. However the method can also be used to display
customized message.
Syntax:-
dictionary_name.pop (key )
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
D = D1.pop ( 3 )
print ( D ) # Mehak
print(D1) # {1: 'Anjali', 2: 'Sristhi'}
9) del:- This command can also be used remove/delete an element from the dictionary using it's key but it
doesn't returns the deleted element.
Syntax:-
del dictionary_name[ key ]
Example:-
D1 = { 1 : "Anjali" , 2 : "Sristhi" , 3 : "Mehak" }
del D1 [ 3 ]
print(D1) # {1: 'Anjali', 2: 'Sristhi'}

#Program - 21 Program in Python to create dictionary of n no of students to store roll no as key and name as
values
std={}
n=int(input("Enter no of students:-"))
for i in range(n):
r=int(input("Enter roll no :- "))
n=input("Enter name :- ")
std[r]=n
print("Dictionary of students :-\n ",std)

#‐--------‐--------------------------------

#Program - 22 Program in Python to create dictionary of n no of students to store name as key and marks as
values and display name & marks of students getting marks greater than 75
std={}
n=int(input("Enter no of students:-"))
for i in range(n):
n=input("Enter name :- ")
m=int(input("Enter marks :- "))
std[n]=m
print("Dictionary of students :-\n ",std)
print("Name & marks of students getting marks>75 :- ")
for i in std:
if std[i]>75:
print(i, std[i])

You might also like