@@ -5,81 +5,90 @@ Last-Modified: $Date$
55Author: barry@python.org (Barry Warsaw)
66Status: Final
77Type: Standards Track
8+ Content-Type: text/x-rst
89Created: 13-Jul-2000
910Python-Version: 2.0
1011Post-History:
1112
1213
1314Introduction
15+ ============
1416
15- This PEP describes a proposed syntactical extension to Python,
16- list comprehensions.
17+ This PEP describes a proposed syntactical extension to Python, list
18+ comprehensions.
1719
1820
1921The Proposed Solution
22+ =====================
2023
21- It is proposed to allow conditional construction of list literals
22- using for and if clauses. They would nest in the same way for
23- loops and if statements nest now.
24+ It is proposed to allow conditional construction of list literals using for and
25+ if clauses. They would nest in the same way for loops and if statements nest
26+ now.
2427
2528
2629Rationale
30+ =========
2731
28- List comprehensions provide a more concise way to create lists in
29- situations where map() and filter() and/or nested loops would
30- currently be used.
32+ List comprehensions provide a more concise way to create lists in situations
33+ where map() and filter() and/or nested loops would currently be used.
3134
3235
3336Examples
37+ ========
38+
39+ ::
3440
3541 >>> print [i for i in range(10)]
3642 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3743
3844 >>> print [i for i in range(20) if i%2 == 0]
3945 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
4046
41- >>> nums = [1,2,3, 4]
47+ >>> nums = [1, 2, 3, 4]
4248 >>> fruit = ["Apples", "Peaches", "Pears", "Bananas"]
43- >>> print [(i,f) for i in nums for f in fruit]
49+ >>> print [(i, f) for i in nums for f in fruit]
4450 [(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'),
4551 (2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'),
4652 (3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'),
4753 (4, 'Apples'), (4, 'Peaches'), (4, 'Pears'), (4, 'Bananas')]
48- >>> print [(i,f) for i in nums for f in fruit if f[0] == "P"]
54+ >>> print [(i, f) for i in nums for f in fruit if f[0] == "P"]
4955 [(1, 'Peaches'), (1, 'Pears'),
5056 (2, 'Peaches'), (2, 'Pears'),
5157 (3, 'Peaches'), (3, 'Pears'),
5258 (4, 'Peaches'), (4, 'Pears')]
53- >>> print [(i,f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
59+ >>> print [(i, f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
5460 [(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')]
55- >>> print [i for i in zip(nums,fruit) if i[0]%2==0]
61+ >>> print [i for i in zip(nums, fruit) if i[0]%2==0]
5662 [(2, 'Peaches'), (4, 'Bananas')]
5763
5864
5965Reference Implementation
66+ ========================
6067
61- List comprehensions become part of the Python language with
62- release 2.0, documented in [1].
68+ List comprehensions become part of the Python language with release 2.0,
69+ documented in [1]_ .
6370
6471
6572BDFL Pronouncements
73+ ===================
74+ * The syntax proposed above is the Right One.
6675
67- - The syntax proposed above is the Right One.
68-
69- - The form [x, y for ...] is disallowed; one is required to write
70- [(x, y) for ...].
76+ * The form ``[x, y for ...]`` is disallowed; one is required to write
77+ ``[(x, y) for ...]``.
7178
72- - The form [... for x... for y...] nests, with the last index
73- varying fastest, just like nested for loops.
79+ * The form `` [... for x... for y...]`` nests, with the last index
80+ varying fastest, just like nested for loops.
7481
7582
7683References
84+ ==========
85+
86+ .. [1] http://docs.python.org/reference/expressions.html#list-displays
7787
78- [1] http://docs.python.org/reference/expressions.html#list-displays
7988
8089
81-
82- Local Variables:
83- mode: indented-text
84- indent-tabs-mode: nil
85- End:
90+ ..
91+ Local Variables:
92+ mode: indented-text
93+ indent-tabs-mode: nil
94+ End:
0 commit comments