Skip to content

Commit e31ad08

Browse files
committed
Iterator
1 parent c0d9170 commit e31ad08

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

README.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,37 @@ Point(x=1, y=2)
169169

170170
Iterator
171171
--------
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+
172179
```python
173180
<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)
175189
```
176190

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
177203
#### Reads input until it reaches an empty line:
178204
```python
179205
for line in iter(input, ''):
@@ -187,12 +213,6 @@ for line in iter(partial(input, 'Please enter value: '), ''):
187213
...
188214
```
189215

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-
196216
#### Skips first item:
197217
```python
198218
next(<iter>)
@@ -219,33 +239,6 @@ def count(start, step):
219239
```
220240

221241

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-
249242
Type
250243
----
251244
```python

0 commit comments

Comments
 (0)