Page 216 -
P. 216

idle session







              Let’s see the Python dictionary in action. Follow along with this IDLE session on your computer, ensuring that you
              get the same results as shown.
              Start by creating two empty dictionaries, one using curly braces and the other using a factory function:
              >>> cleese = {}
              >>> palin = dict()        Both techniques create
              >>> type(cleese)          an empty dictionary, as
              <class 'dict'>            confirmed.
              >>> type(palin)
              <class 'dict'>

              Add some data to both of these dictionaries by associating values with keys. Note the actual structure of the data
              is presenting itself here, as each dictionary has a Name and a list of Occupations. Note also that the palin
              dictionary is being created at the same time:

              >>> cleese['Name'] = 'John Cleese'
              >>> cleese['Occupations'] = ['actor', 'comedian', 'writer', 'film producer']
              >>> palin = {'Name': 'Michael Palin', 'Occupations': ['comedian', 'actor', 'writer', 'tv']}

              With your data associated with keys (which are strings, in this case), it is possible to access an individual data item
              using a notation similar to that used with lists:
                                        Use square brackets to index into the dictionary to access
              >>> palin['Name']         data items, but instead of numbers, index with keys.
             'Michael Palin'                    Use numbers to access a list item stored at a particular dictionary key.
              >>> cleese['Occupations'][-1]     Think of this as “index-chaining” and read from right to left: “…the last
             'film producer'                    item of the list associated with Occupations…”.
              As with lists, a Python dictionary can grow dynamically to store additional key/value pairings. Let’s add some data
              about birthplace to each dictionary:                                  Provide the data associated

              >>> palin['Birthplace'] = "Broomhill, Sheffield, England"            with the new key.
              >>> cleese['Birthplace'] = "Weston-super-Mare, North Somerset, England"

              Unlike lists, a Python dictionary does not maintain insertion order, which can result in some unexpected
              behavior. The key point is that the dictionary maintains the associations, not the ordering:

              >>> palin
              {'Birthplace': 'Broomhill, Sheffield, England', 'Name': 'Michael Palin', 'Occupations':
              ['comedian', 'actor', 'writer', 'tv']}
              >>> cleese
              {'Birthplace': 'Weston-super-Mare, North Somerset, England', 'Name': 'John Cleese',
             'Occupations': ['actor', 'comedian', 'writer', 'film producer']}
                                    The ordering maintained by Python is different from how the data
                                     was inserted. Don’t worry about it; this is OK.



           180    Chapter 6
   211   212   213   214   215   216   217   218   219   220   221