@@ -5,81 +5,90 @@ Last-Modified: $Date$
5
5
Author:
[email protected] (Barry Warsaw)
6
6
Status: Final
7
7
Type: Standards Track
8
+ Content-Type: text/x-rst
8
9
Created: 13-Jul-2000
9
10
Python-Version: 2.0
10
11
Post-History:
11
12
12
13
13
14
Introduction
15
+ ============
14
16
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.
17
19
18
20
19
21
The Proposed Solution
22
+ =====================
20
23
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.
24
27
25
28
26
29
Rationale
30
+ =========
27
31
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.
31
34
32
35
33
36
Examples
37
+ ========
38
+
39
+ ::
34
40
35
41
>>> print [i for i in range(10)]
36
42
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
37
43
38
44
>>> print [i for i in range(20) if i%2 == 0]
39
45
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
40
46
41
- >>> nums = [1,2,3, 4]
47
+ >>> nums = [1, 2, 3, 4]
42
48
>>> 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]
44
50
[(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'),
45
51
(2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'),
46
52
(3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'),
47
53
(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"]
49
55
[(1, 'Peaches'), (1, 'Pears'),
50
56
(2, 'Peaches'), (2, 'Pears'),
51
57
(3, 'Peaches'), (3, 'Pears'),
52
58
(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]
54
60
[(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]
56
62
[(2, 'Peaches'), (4, 'Bananas')]
57
63
58
64
59
65
Reference Implementation
66
+ ========================
60
67
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]_ .
63
70
64
71
65
72
BDFL Pronouncements
73
+ ===================
74
+ * The syntax proposed above is the Right One.
66
75
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 ...]``.
71
78
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.
74
81
75
82
76
83
References
84
+ ==========
85
+
86
+ .. [1] http://docs.python.org/reference/expressions.html#list-displays
77
87
78
- [1] http://docs.python.org/reference/expressions.html#list-displays
79
88
80
89
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