@@ -169,11 +169,37 @@ Point(x=1, y=2)
169
169
170
170
Iterator
171
171
--------
172
+ * ** If you want to print the iterator, you need to pass it to the list() function.**
173
+ * ** In this cheatsheet ` '<collection>' ` can also mean an iterator.**
174
+
175
+ ``` python
176
+ from itertools import islice, count, repeat, cycle, chain
177
+ ```
178
+
172
179
``` python
173
180
< iter > = iter (< collection> )
174
- < iter > = iter (< function> , to_exclusive)
181
+ < iter > = iter (< function> , to_exclusive) # Sequence of return values until 'to_exclusive'.
182
+ < el> = next (< iter > [, default]) # Raises StopIteration or returns 'default' on end.
183
+ ```
184
+
185
+ ``` python
186
+ < iter > = islice(< collection> , to_exclusive)
187
+ < iter > = islice(< collection> , from_inclusive, to_exclusive)
188
+ < iter > = islice(< collection> , from_inclusive, to_exclusive, step_size)
175
189
```
176
190
191
+ ``` python
192
+ < iter > = count(start = 0 , step = 1 ) # Returns incremented integer endlessly.
193
+ < iter > = repeat(< el> [, times]) # Returns element endlessly or 'times' times.
194
+ < iter > = cycle(< collection> ) # Repeats the sequence indefinitely.
195
+ ```
196
+
197
+ ``` python
198
+ < iter > = chain(< collection> , < collection> ) # Empties collections in order.
199
+ < iter > = chain.from_iterable(< collection> ) # Empties collections inside a collection in order.
200
+ ```
201
+
202
+ ### Examples
177
203
#### Reads input until it reaches an empty line:
178
204
``` python
179
205
for line in iter (input , ' ' ):
@@ -187,12 +213,6 @@ for line in iter(partial(input, 'Please enter value: '), ''):
187
213
...
188
214
```
189
215
190
- ### Next
191
- ** Returns next item. If there are no more items it raises StopIteration exception or returns default if specified.**
192
- ``` python
193
- < el> = next (< iter > [, default])
194
- ```
195
-
196
216
#### Skips first item:
197
217
``` python
198
218
next (< iter > )
@@ -219,33 +239,6 @@ def count(start, step):
219
239
```
220
240
221
241
222
- Itertools
223
- ---------
224
- * ** Every function returns an iterator and can accept any collection and/or iterator.**
225
- * ** If you want to print the iterator, you need to pass it to the list() function!**
226
-
227
- ``` python
228
- from itertools import islice, count, repeat, cycle, chain
229
- ```
230
-
231
- ``` python
232
- < iter > = islice(< collection> , to_exclusive)
233
- < iter > = islice(< collection> , from_inclusive, to_exclusive)
234
- < iter > = islice(< collection> , from_inclusive, to_exclusive, step_size)
235
- ```
236
-
237
- ``` python
238
- < iter > = count(start = 0 , step = 1 ) # Counter.
239
- < iter > = repeat(< el> [, times]) # Returns element endlessly or times times.
240
- < iter > = cycle(< collection> ) # Repeats the sequence indefinitely.
241
- ```
242
-
243
- ``` python
244
- < iter > = chain(< collection> , < collection> ) # Empties sequences in order.
245
- < iter > = chain.from_iterable(< collection> ) # Empties sequences inside a sequence in order.
246
- ```
247
-
248
-
249
242
Type
250
243
----
251
244
``` python
0 commit comments