2000-07-13 02:33:08 -04:00
|
|
|
|
PEP: 202
|
|
|
|
|
Title: List Comprehensions
|
|
|
|
|
Version: $Revision$
|
2006-03-23 15:13:19 -05:00
|
|
|
|
Last-Modified: $Date$
|
2003-09-22 00:51:50 -04:00
|
|
|
|
Author: barry@python.org (Barry Warsaw)
|
2001-08-14 14:43:06 -04:00
|
|
|
|
Status: Final
|
2000-08-23 01:19:21 -04:00
|
|
|
|
Type: Standards Track
|
2016-10-23 15:15:09 -04:00
|
|
|
|
Content-Type: text/x-rst
|
2000-08-23 01:19:21 -04:00
|
|
|
|
Created: 13-Jul-2000
|
2007-06-19 00:20:07 -04:00
|
|
|
|
Python-Version: 2.0
|
2000-08-23 01:19:21 -04:00
|
|
|
|
Post-History:
|
2000-07-13 02:33:08 -04:00
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2000-07-25 11:07:28 -04:00
|
|
|
|
Introduction
|
2016-10-23 15:15:09 -04:00
|
|
|
|
============
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
This PEP describes a proposed syntactical extension to Python, list
|
|
|
|
|
comprehensions.
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2000-07-25 11:07:28 -04:00
|
|
|
|
The Proposed Solution
|
2016-10-23 15:15:09 -04:00
|
|
|
|
=====================
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
It is proposed to allow conditional construction of list literals using for and
|
|
|
|
|
if clauses. They would nest in the same way for loops and if statements nest
|
|
|
|
|
now.
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
2016-10-23 15:15:09 -04:00
|
|
|
|
=========
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
List comprehensions provide a more concise way to create lists in situations
|
|
|
|
|
where map() and filter() and/or nested loops would currently be used.
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2000-07-25 11:07:28 -04:00
|
|
|
|
Examples
|
2016-10-23 15:15:09 -04:00
|
|
|
|
========
|
|
|
|
|
|
|
|
|
|
::
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
|
|
|
|
>>> print [i for i in range(10)]
|
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
|
|
|
|
|
>>> print [i for i in range(20) if i%2 == 0]
|
|
|
|
|
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
|
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
>>> nums = [1, 2, 3, 4]
|
2000-07-25 11:07:28 -04:00
|
|
|
|
>>> fruit = ["Apples", "Peaches", "Pears", "Bananas"]
|
2016-10-23 15:15:09 -04:00
|
|
|
|
>>> print [(i, f) for i in nums for f in fruit]
|
2000-07-25 11:07:28 -04:00
|
|
|
|
[(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'),
|
|
|
|
|
(2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'),
|
|
|
|
|
(3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'),
|
|
|
|
|
(4, 'Apples'), (4, 'Peaches'), (4, 'Pears'), (4, 'Bananas')]
|
2016-10-23 15:15:09 -04:00
|
|
|
|
>>> print [(i, f) for i in nums for f in fruit if f[0] == "P"]
|
2000-07-25 11:07:28 -04:00
|
|
|
|
[(1, 'Peaches'), (1, 'Pears'),
|
|
|
|
|
(2, 'Peaches'), (2, 'Pears'),
|
|
|
|
|
(3, 'Peaches'), (3, 'Pears'),
|
|
|
|
|
(4, 'Peaches'), (4, 'Pears')]
|
2016-10-23 15:15:09 -04:00
|
|
|
|
>>> print [(i, f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
|
2000-07-25 11:07:28 -04:00
|
|
|
|
[(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')]
|
2016-10-23 15:15:09 -04:00
|
|
|
|
>>> print [i for i in zip(nums, fruit) if i[0]%2==0]
|
2000-07-25 11:07:28 -04:00
|
|
|
|
[(2, 'Peaches'), (4, 'Bananas')]
|
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2000-07-25 11:07:28 -04:00
|
|
|
|
Reference Implementation
|
2016-10-23 15:15:09 -04:00
|
|
|
|
========================
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
List comprehensions become part of the Python language with release 2.0,
|
|
|
|
|
documented in [1]_.
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
BDFL Pronouncements
|
2016-10-23 15:15:09 -04:00
|
|
|
|
===================
|
|
|
|
|
* The syntax proposed above is the Right One.
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
* The form ``[x, y for ...]`` is disallowed; one is required to write
|
|
|
|
|
``[(x, y) for ...]``.
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
* The form ``[... for x... for y...]`` nests, with the last index
|
|
|
|
|
varying fastest, just like nested for loops.
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
|
|
|
|
|
2000-08-23 01:19:21 -04:00
|
|
|
|
References
|
2016-10-23 15:15:09 -04:00
|
|
|
|
==========
|
|
|
|
|
|
|
|
|
|
.. [1] http://docs.python.org/reference/expressions.html#list-displays
|
2000-08-23 01:19:21 -04:00
|
|
|
|
|
2000-07-25 11:07:28 -04:00
|
|
|
|
|
2000-07-27 16:13:39 -04:00
|
|
|
|
|
2016-10-23 15:15:09 -04:00
|
|
|
|
..
|
|
|
|
|
Local Variables:
|
|
|
|
|
mode: indented-text
|
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
|
End:
|