Difference between revisions of "Dictionary"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(add content)
(Retrieval: add info about KeyError)
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
A '''dictionary''' is an unordered collection of key-value pairs. The keys of a dictionary must be unique and [[immutable]].
+
{{C-class}}
 +
In [[Python]], a '''dictionary''' is an unordered collection of key-value pairs. The keys of a dictionary must be unique and [[immutable]].
  
 
== Creation ==
 
== Creation ==
Line 12: Line 13:
 
{1: 1, 2: 4, 3: 9}
 
{1: 1, 2: 4, 3: 9}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
In addition, a dictionary can be created by a [[dictionary comprehension]].
  
 
== Length ==
 
== Length ==
Line 36: Line 39:
  
 
== Retrieval ==
 
== Retrieval ==
To retrieve the value associated with key <code>k</code>, use <code>d[k]</code>. Example:
+
To retrieve the value associated with key <code>k</code>, use <code>d[k]</code>. If <code>k</code> is not a key in the dictionary, a <code>KeyError</code> is raised. Example:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
>>> d = {1: 1, 2: 4, 3: 9}
 
>>> d = {1: 1, 2: 4, 3: 9}
 
>>> d[2]
 
>>> d[2]
 
4
 
4
 +
>>> d[4]
 +
Traceback (most recent call last):
 +
  ...
 +
KeyError: 4
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 75: Line 82:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
To iterate over the key-value pairs of a dictionary, use:
+
To iterate over the (key, value) pairs of a dictionary, use:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
for key, value in d.items():
 
for key, value in d.items():

Latest revision as of 19:10, 11 June 2014

In Python, a dictionary is an unordered collection of key-value pairs. The keys of a dictionary must be unique and immutable.

Creation

A dictionary is created by enclosing comma-separated key: value mappings in braces:

dict = {key0: val0, key1: val1}
An empty dictionary is constructed with {}.

Alternatively, a dictionary can be constructed by dict(s), for a sequence s of key-value pairs. Example:

>>> dict([(1, 1), (2, 4), (3, 9)])
{1: 1, 2: 4, 3: 9}

In addition, a dictionary can be created by a dictionary comprehension.

Length

The number of key-value pairs in dictionary d is given by len(d).

Addition

A key-value pair is added to dictionary d by d[key] = value. Example:

>>> d = {1: 1, 2: 4, 3: 9}
>>> d[4] = 16
>>> d
{1: 1, 2: 4, 3: 9, 4: 16}

If key already exists in d, the existing value is overwritten with value.

Deletion

To delete a key-value pair, use del d[key]. Example:

>>> d = {1: 1, 2: 4, 3: 9}
>>> del d[1]
>>> d
{2: 4, 3: 9}

Retrieval

To retrieve the value associated with key k, use d[k]. If k is not a key in the dictionary, a KeyError is raised. Example:

>>> d = {1: 1, 2: 4, 3: 9}
>>> d[2]
4
>>> d[4]
Traceback (most recent call last):
  ...
KeyError: 4

Key membership test

To test if key k is a key in the dictionary, use k in d. Example:

>>> d = {1: 1, 2: 4, 3: 9}
>>> 3 in d
True

Iterators

There are several dictionary methods that return iterators over parts of a dictionary. For dictionary d:

  • d.keys() returns an iterator over the keys.
  • d.values() returns an iterator over the values.
  • d.items() returns an iterator over the (key, value) pairs.

Example:

>>> d = {1: 1, 2: 4, 3: 9}
>>> d.keys()
dict_keys([1, 2, 3])
>>> d.values()
dict_values([1, 4, 9])
>>> d.items()
dict_items([(1, 1), (2, 4), (3, 9)])

Iteration techniques

To iterate over the keys of a dictionary use:

for key in d:
    ...

To iterate over the (key, value) pairs of a dictionary, use:

for key, value in d.items():
    ...

To iterate over the values of a dictionary, use:

for value in d.values():
    ...

Sources