Home > Articles

  • Print
  • + Share This
This chapter is from the book

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

Assignment by key

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 preceding assignments identical to the following:

d[(1,2,3)] = "foo"
d[(1,0,3)] = "bar"
  • + Share This
  • 🔖 Save To Your Account