O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mapping Types

A mapping object maps values of one type (the key type) to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary . A dictionary’s keys are almost arbitrary values. The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (e.g., 1 and 1.0) then they can be used interchangeably to index the same dictionary entry.

Dictionaries are created by placing a comma-separated list of key: value pairs within braces, for example: {'jack': 4098, 'sjoerd': 4127} or {4098: 'jack', 4127: 'sjoerd'}.

The operations in the following table are defined on mappings (where a is a mapping, k is a key and x is an arbitrary object).

Operation

Result

Notes

len(a)

The number of items in a

 

a[k]

The item of a with key k

1

a[k] = x

Set a[k] to x

 

del a[k]

Remove a[k] from a (1)

 

a.clear()

Remove all items from a

 

a.copy()

A (shallow) copy of a

 

a.has_key(k)

1 if a has a key k, else 0

 

a.items()

A copy of a’s list of (key, value) pairs

2

a.keys()

A copy of a’s list of keys

2

a.update(b)

For k, v in b.items(): a[k] = v

3

a.values()

A copy of a’s list of values

2

a.get(k[, f])

The value of a with key k

4

Notes

  1. This raises an exception if k is not in the map.

  2. Keys ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required