You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: comprehensions.rst
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,7 @@ comprehensions are supported in both Python 2 and Python 3:
9
9
- list comprehensions
10
10
- dictionary comprehensions
11
11
- set comprehensions
12
+
- generator comprehensions
12
13
13
14
We will discuss them one by one. Once you get the hang of using ``list``
14
15
comprehensions then you can use any of them easily.
@@ -91,3 +92,17 @@ that they use braces ``{}``. Here is an example:
91
92
squared = {x**2for x in [1, 1, 2]}
92
93
print(squared)
93
94
# Output: {1, 4}
95
+
96
+
``generator`` comprehensions
97
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
98
+
99
+
They are also similar to list comprehensions. The only difference is that they don't allocate memory for the whole list but generate one item at a time, thus more memory effecient.
100
+
101
+
.. code:: python
102
+
103
+
multiples_gen = (i for i inrange(30) if i %3==0)
104
+
print(multiples_gen)
105
+
# Output: <generator object <genexpr> at 0x7fdaa8e407d8>
0 commit comments