Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Oct 19, 2023
1 parent e846d72 commit 4d16a18
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ cmd = shlex.join(args)

Categories: `itertools` `performance` `readability`

When flattening an list of lists, use the `chain.from_iterable` function
When flattening a list of lists, use the `chain.from_iterable()` function
from the `itertools` stdlib package. This function is faster than native
list/generator comprehensions or using `sum()` with a list default.

Expand Down Expand Up @@ -2083,9 +2083,13 @@ from itertools import chain

rows = [[1, 2], [3, 4]]

flat = chain.from_iterable(*rows)
flat = chain.from_iterable(rows)
```

Note: `chain.from_iterable()` returns an iterator, which means you might
need to wrap it in `list()` depending on your use case. Refurb cannot
detect this (yet), so this is something you will need to keep in mind.

Note: `chain(*x)` may be marginally faster/slower depending on the length
of `x`. Since `*` might potentially expand to a lot of arguments, it is
better to use `chain.from_iterable()` when you are unsure.
Expand Down

0 comments on commit 4d16a18

Please sign in to comment.