[Python-3000-checkins] r57971 - python/branches/py3k/Doc/library/stdtypes.rst
georg.brandl
python-3000-checkins at python.org
Tue Sep 4 19:58:03 CEST 2007
Author: georg.brandl
Date: Tue Sep 4 19:58:02 2007
New Revision: 57971
Modified:
python/branches/py3k/Doc/library/stdtypes.rst
Log:
Add a dict view usage example.
Modified: python/branches/py3k/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/py3k/Doc/library/stdtypes.rst (original)
+++ python/branches/py3k/Doc/library/stdtypes.rst Tue Sep 4 19:58:02 2007
@@ -1906,6 +1906,36 @@
four operations will fail if an involved dictionary contains such a value.
+An example of dictionary view usage::
+
+ >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
+ >>> keys = dishes.keys()
+ >>> values = dishes.values()
+
+ >>> # iteration
+ >>> n = 0
+ >>> for val in values:
+ ... n += val
+ >>> print(n)
+ 504
+
+ >>> # keys and values are iterated over in the same order
+ >>> list(keys)
+ ['eggs', 'bacon', 'sausage', 'spam']
+ >>> list(values)
+ [2, 1, 1, 500]
+
+ >>> # view objects are dynamic and reflect dict changes
+ >>> del dishes['eggs']
+ >>> del dishes['sausage']
+ >>> list(keys)
+ ['spam', 'bacon']
+
+ >>> # set operations
+ >>> keys & {'eggs', 'bacon', 'salad'}
+ {'eggs', 'bacon'}
+
+
.. _bltin-file-objects:
File Objects
More information about the Python-3000-checkins
mailing list