Page 211 - Information_Practice_Fliipbook_Class11
P. 211

'person1': {'name': 'Alice', 'age': 25, 'gender': 'female'},
                'person2': {'name': 'Bob', 'age': 30, 'gender': 'male'}
            }
            In this example, myDict is a dictionary that contains two keys, 'person1' and 'person2'. The value for each of
            these keys is another dictionary, which contains information about each person.
            To access a value in a nested dictionary, we can use multiple square brackets to drill down to the desired level. For
            example, to access the name of person1 in the above example, you can do:

            name = myDict['person1']['name']
            print(name)
            Sample Output:
            'Alice'
            In this example, myDict['person1'] returns the inner dictionary for 'person1', and myDict['person1']
            ['name'] returns the value associated with the 'name' key in that inner dictionary.


            8.4 Dictionary Methods

            Python provides several methods for manipulating dictionaries. Unlike strings and tuples, a dictionary is a mutable
            object. To invoke a method associated with a dictionary object, the dict object is followed by a dot, followed by the
            name of the method, followed by a pair of parentheses that encloses the arguments (if any) required for invoking the
            method. Using a dictionary object  myDict, we describe some methods that apply to the dictionaries:
            ●  myDict.keys(): The method keys() returns a dict_keys object comprising all the keys included in the
              dictionary. We can iterate over the keys in the  dict_keys  object and also check for membership of a key.
              However, dict_keys does not support indexing.
            Example:

             >>> subjects
                 {'Sanskrit': 78, 'English': 185, 'Maths': 88, 'Hindi': 90}
             >>> subjects.keys()
                 dict_keys(['Sanskrit', 'English', 'Maths', 'Hindi'])
             >>> 'English' in subjects.keys()
                 True
             >>> for subject in subjects.keys():
             ...      print(subject, end = ' ')
             ...
             ...
                 Sanskrit English Maths Hindi
             >>> subjects.keys()[0]
                 Traceback (most recent call last):
                   File "<pyshell#38>", line 1, in <module>
                     subjects.keys()[0]
                 TypeError: 'dict_keys' object is not subscriptable


             C T  01     Consider the dictionary, myDict.

                         >>> myDict = { 'b':'beta', 'g':'gamma', 'a':'alpha' }
                         Display all the keys of myDict.



            ●  myDict.values(): The method values() returns a dict_values object comprising all the values included
              in the dictionary. We can iterate over the values in the  dict_values object and also check for membership of
              a value in a dict_values. However,  dict_values does not support indexing.



                                                                                             Python Dictionaries  197
   206   207   208   209   210   211   212   213   214   215   216