diff --git a/docs/checks.md b/docs/checks.md index a1148a4..c8a1846 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -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. @@ -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.