diff options
author | Georg Brandl <georg@python.org> | 2007-09-04 17:58:02 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-09-04 17:58:02 (GMT) |
commit | c53c9666f57b83ac0430ce6ca480b58e011e96ea (patch) | |
tree | 7e4126c16f9fc7a820a96571c46d35511b953fe6 /Doc | |
parent | 952aea2ce6e763c238539e5314885f2938518a4d (diff) | |
download | cpython-c53c9666f57b83ac0430ce6ca480b58e011e96ea.zip cpython-c53c9666f57b83ac0430ce6ca480b58e011e96ea.tar.gz cpython-c53c9666f57b83ac0430ce6ca480b58e011e96ea.tar.bz2 |
Add a dict view usage example.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/stdtypes.rst | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 7ec0180..75db75c 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1906,6 +1906,36 @@ to another dictionary view or a set): 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 |