[Python-checkins] CVS: python/nondist/peps pep-0274.txt,NONE,1.1

Barry Warsaw bwarsaw@users.sourceforge.net
Thu, 25 Oct 2001 13:28:07 -0700


Update of /cvsroot/python/python/nondist/peps
In directory usw-pr-cvs1:/tmp/cvs-serv11455

Added Files:
	pep-0274.txt 
Log Message:
PEP 274, Dict Comprehensions, Warsaw


--- NEW FILE: pep-0274.txt ---
PEP: 274
Title: Dict Comprehensions
Version: $Revision: 1.1 $
Last-Modified: $Date: 2001/10/25 20:28:05 $
Author: barry@zope.com (Barry A. Warsaw)
Status: Draft
Type: Standards Track
Created: 25-Oct-2001
Python-Version: 2.3
Post-History:


Abstract

    PEP 202 introduces a syntactical extension to Python called the
    "list comprehension"[1].  This PEP proposes a similar syntactical
    extension called the "dictionary comprehension" or "dict
    comprehension" for short.  You can use dict comprehensions in ways
    very similar to list comprehensions, except that they produce
    Python dictionary objects instead of list objects.


Proposed Solution

    Dict comprehensions are just like list comprehensions, except that
    you group the expression using curly braces instead of square
    braces.  Also, the left part before the `for' keyword expresses
    both a key and a value, separated by a colon.  (There is an
    optional part of this PEP that allows you to use a shortcut to
    express just the value.)  The notation is specifically designed to
    remind you of list comprehensions as applied to dictionaries.


Rationale

    There are times when you have some data arranged as a sequences of
    length-2 sequences, and you want to turn that into a dictionary.
    In Python 2.2, the dictionary() constructor will take an optional
    keyword argument that indicates specifically to interpret a
    sequences of length-2 sequences as key/value pairs, and turn them
    into a dictionary.

    However, the act of turning some data into a sequence of length-2
    sequences can be inconvenient or inefficient from a memory or
    performance standpoint.  Also, for some common operations, such as
    turning a list of things into a set of things for quick duplicate
    removal or set inclusion tests, a better syntax can help code
    clarity.

    As with list comprehensions, an explicit for loop can always be
    used (and in fact was the only way to do it in earlier versions of
    Python).  But as with list comprehensions, dict comprehensions can
    provide a more syntactically succinct idiom that the traditional
    for loop.


Examples

    >>> print {i : chr(65+i) for i in range(4)}
    {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}

    >>> print {k : v for k, v in someDict.items()} == someDict.copy()
    1

    >>> print {x.lower() : 1 for x in list_of_email_addrs}
    {'barry@zope.com'   : 1, 'barry@python.org' : 1, 'guido@python.org' : 1}

    >>> def invert(d):
    ...     return {v : k for k, v in d}
    ...
    >>> d = {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}
    >>> print invert(d)
    {'A' : 0, 'B' : 1, 'C' : 2, 'D' : 4}

    >>> print {k, v for k in range(4) for v in range(-4, 0, 1)}
    {0 : -4, 1 : -3, 2 : -2, 3 : -1}


Optional Enhancements

    There is one further shortcut we could adopt.  Suppose we wanted
    to create a set of items, such as in the "list_of_email_addrs"
    example above.  Here, we're simply taking the target of the for
    loop and turning that into the key for the dict comprehension.
    The assertion is that this would be a common idiom, so the
    shortcut below allows for an easy spelling of it, by allow us to
    omit the "key :" part of the left hand clause:

    >>> print {1 for x in list_of_email_addrs}
    {'barry@zope.com'   : 1, 'barry@python.org' : 1, 'guido@python.org' : 1}

    Or say we wanted to map email addresses to the MX record handling
    their mail:

    >>> print {mx_for_addr(x) for x in list_of_email_addrs}
    {'barry@zope.com'   : 'mail.zope.com',
     'barry@python.org' : 'mail.python.org,
     'guido@python.org' : 'mail.python.org,
     }


References

    [1] PEP 202, List Comprehensions
        http://www.python.org/peps/pep-0202.html


Copyright

    This document has been placed in the public domain.



Local Variables:
mode: indented-text
indent-tabs-mode: nil
fill-column: 70
End: