Linux/Unix > Python Essential Reference, Second Edition > 4. Operators and Expressions > Operations on Dictionaries | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104030195077107231032185

### Operations on Dictionaries

Dictionaries provide a mapping between names and objects. You can apply the following operations to dictionaries:

Operation | Description
--- | ---
`x` `=` `d` `[``k` `]` | Indexing by key
`d` `[``k` `] =` `x` | Key assignment
`del` `d` `[``k` `]` | Deletes an item by key
`len(``d` `)` | Number of items in the dictionary

Key values can be any immutable object, such as strings, numbers, and tuples. In addition, dictionary keys can be specified as a comma-separated list of values like this:

    d = { }
    d[1,2,3] = "foo"
    d[1,0,3] = "bar"

In this case, the key values represent a tuple, making the above assignments identical to the following:

    d[(1,2,3)] = "foo"
    d[(1,0,3)] = "bar"

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.