14.12 Accessing and Updating Dictionary Items
A specific value may be accessed or modified using key subscript syntax to reference the corresponding key. The following code references a key known to be in the bookDict dictionary and outputs the associated value (in this case the book entitled “A Tale of Two Cities”):
print(bookDict["200-532874"])
When accessing dictionary entries in this way, it is also possible to declare a default value to be used in the event that the specified key does not return a value:
print(bookDict["999-546547", default: "Book not found"])
Since the dictionary does not contain an entry for the specified key, the above code will output text which reads “Book not found”.
Indexing by key may also be used when updating the value associated with a specified key, for example, to change the title of the same book from “A Tale of Two Cities” to “Sense and Sensibility”...