home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Mapping Types -- Python library reference</TITLE>
- Next: <A HREF="../o/other_built-in_types" TYPE="Next">Other Built-in Types</A>
- Prev: <A HREF="../s/sequence_types" TYPE="Prev">Sequence Types</A>
- Up: <A HREF="../t/types" TYPE="Up">Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>2.1.6. Mapping Types</H2>
- A <DFN>mapping</DFN> 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 <DFN>dictionary</DFN>. 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.
- <P>
- Dictionaries are created by placing a comma-separated list of
- <CODE><VAR>key</VAR>:,<VAR>value</VAR></CODE> pairs within braces, for example:
- <CODE>{'jack':,4098, 'sjoerd':,4127}</CODE> or
- <CODE>{4098:,'jack', 4127:,'sjoerd'}</CODE>.
- <P>
- The following operations are defined on mappings (where <VAR>a</VAR> is a
- mapping, <VAR>k</VAR> is a key and <VAR>x</VAR> is an arbitrary object):
- <P>
- <DL>
- <DT><I>Operation</I><DD><I>Result</I> --- <I>Notes</I>
- <P>
- <DT><CODE>len(<VAR>a</VAR>)</CODE><DD>the number of items in <VAR>a</VAR>
- <DT><CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE><DD>the item of <VAR>a</VAR> with key <VAR>k</VAR> --- (1)
- <DT><CODE><VAR>a</VAR>[<VAR>k</VAR>] = <VAR>x</VAR></CODE><DD>set <CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE> to <VAR>x</VAR>
- <DT><CODE>del <VAR>a</VAR>[<VAR>k</VAR>]</CODE><DD>remove <CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE> from <VAR>a</VAR> --- (1)
- <DT><CODE><VAR>a</VAR>.items()</CODE><DD>a copy of <VAR>a</VAR>'s list of (key, item) pairs --- (2)
- <DT><CODE><VAR>a</VAR>.keys()</CODE><DD>a copy of <VAR>a</VAR>'s list of keys --- (2)
- <DT><CODE><VAR>a</VAR>.values()</CODE><DD>a copy of <VAR>a</VAR>'s list of values --- (2)
- <DT><CODE><VAR>a</VAR>.has_key(<VAR>k</VAR>)</CODE><DD><CODE>1</CODE> if <VAR>a</VAR> has a key <VAR>k</VAR>, else <CODE>0</CODE>
- </DL>
- Notes:
- <DL>
- <DT><B>(1)</B><DD>Raises an exception if <VAR>k</VAR> is not in the map.
- <P>
- <DT><B>(2)</B><DD>Keys and values are listed in random order.
- </DL>
-