Page 425 - Computer Science V2.0 Class 11
P. 425

...
                  ...
                      ('Sanskrit', 78) ('English', 185) ('Maths', 88) ('Hindi', 90)
                 Fig 14.2 shows the keys, values, and items for the dictionary subjects.


























                                      Fig 14.2: key part, value part, and items of object subjects of type dict
                 ●  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 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'}
                 ●  myDict.popitem(): The method    popitem()  removes  the  last  inserted  item  (key-value  pair)  from  the
                   dictionary. In Python versions prior to 3.7, this method removes a random item.
                  Example:
                  >>> months = {3:'March', 11:'November', 12: 'December', 1:'January'}
                  >>> months.popitem()
                      (1, 'January')
                  >>> months
                      {3: 'March', 11: 'November', 12: 'December'}
                 14.5.1 del Operator

                 The Python keyword operator del can also be used to remove key-value pair(s) from a dictionary.
                  Example:

                  >>> months = {3:'March', 11:'November', 12: 'December', 1:'January'}
                  >>> del months[11]


                                                                                                        Dictionaries  411
   420   421   422   423   424   425   426   427   428   429   430