Page 212 - Information_Practice_Fliipbook_Class11
P. 212
Example:
>>> 78 in subjects.values()
True
>>> for subjectCode in subjects.values():
... print(subjectCode, end=' ')
...
...
78 185 88 90
● myDict.items(): The method items() returns a dict_items object comprising the set of items included
in the dictionary. We can check for membership of an item in a dict_items and iterate over the items in the
dict_items object. However, dict_items does not support indexing.
Example:
>>> subjects = {'Sanskrit': 78, 'English': 185, 'Maths': 88, 'Hindi': 90}
>>> ('English', 185) in subjects.items()
True
>>> for subject in subjects.items():
... print(subject, end=' ')
...
...
('Sanskrit', 78) ('English', 185) ('Maths', 88) ('Hindi', 90)
Fig 8.2: key part, value part, and items of object subjects of type dict
4. myDict.update(): The method update() adds all key-value pairs of another dictionary in an existing
dictionary.
Example:
>>> loginDetails = {'Aryan':'a7@101', 'Sunpreet':'sun@102', 'Anthony':'ant@103' }
>>> moreUsers = {'Samantha':'sam@104','Venkatesh':'ven@105'}
>>> loginDetails.update(moreUsers)
>>> print(loginDetails)
{'Aryan': 'a7@101', 'Sunpreet': 'sun@102', 'Anthony': 'ant@103', 'Samantha': 'sam@104',
'Venkatesh': 'ven@105'}
● myDict.pop(myKey): The method pop()returns the value (say, myValue) for the key (myKey)passed as an
argument and removes the item (myKey:myValue) with the specified key from the dictionary.
Example:
>>> months = {3:'March', 11:'November', 12: 'December', 1:'January'}
>>> months.pop(11)
'November'
>>> months
{3: 'March', 12: 'December', 1: 'January'}
198 Touchpad Informatics Practices-XI

