diff --git a/README.md b/README.md index f3de31f..fc48243 100644 --- a/README.md +++ b/README.md @@ -169,11 +169,37 @@ Point(x=1, y=2) Iterator -------- +* **If you want to print the iterator, you need to pass it to the list() function.** +* **In this cheatsheet `''` can also mean an iterator.** + +```python +from itertools import islice, count, repeat, cycle, chain +``` + ```python = iter() - = iter(, to_exclusive) + = iter(, to_exclusive) # Sequence of return values until 'to_exclusive'. + = next( [, default]) # Raises StopIteration or returns 'default' on end. +``` + +```python + = islice(, to_exclusive) + = islice(, from_inclusive, to_exclusive) + = islice(, from_inclusive, to_exclusive, step_size) ``` +```python + = count(start=0, step=1) # Returns incremented integer endlessly. + = repeat( [, times]) # Returns element endlessly or 'times' times. + = cycle() # Repeats the sequence indefinitely. +``` + +```python + = chain(, ) # Empties collections in order. + = chain.from_iterable() # Empties collections inside a collection in order. +``` + +### Examples #### Reads input until it reaches an empty line: ```python for line in iter(input, ''): @@ -187,12 +213,6 @@ for line in iter(partial(input, 'Please enter value: '), ''): ... ``` -### Next -**Returns next item. If there are no more items it raises StopIteration exception or returns default if specified.** -```python - = next( [, default]) -``` - #### Skips first item: ```python next() @@ -219,33 +239,6 @@ def count(start, step): ``` -Itertools ---------- -* **Every function returns an iterator and can accept any collection and/or iterator.** -* **If you want to print the iterator, you need to pass it to the list() function!** - -```python -from itertools import islice, count, repeat, cycle, chain -``` - -```python - = islice(, to_exclusive) - = islice(, from_inclusive, to_exclusive) - = islice(, from_inclusive, to_exclusive, step_size) -``` - -```python - = count(start=0, step=1) # Counter. - = repeat( [, times]) # Returns element endlessly or times times. - = cycle() # Repeats the sequence indefinitely. -``` - -```python - = chain(, ) # Empties sequences in order. - = chain.from_iterable() # Empties sequences inside a sequence in order. -``` - - Type ---- ```python