---
title: "Comment by Hynek Černoch on \"7 Different Ways to Flatten a List of Lists in Python\" | Hashnode"
description: "All functions measured by a line %timeit some_function(l) are measured incorrectly. A time in about hundreds of nanoseconds to get a list of 9 * 1000 numbers is"
url: "https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdc3422b78ac62dff80aa9a"
---

[Discussion](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7)

Comment [7 Different Ways to Flatten a List of Lists in Python](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7)

[Hynek Černoch](https://hashnode.com/@hynekcer)

[Dec 18, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdc3422b78ac62dff80aa9a)

All functions measured by a line `%timeit some_function(l)` are measured incorrectly. A time in about hundreds of nanoseconds to get a list of 9 \* 1000 numbers is a nonsense. You will see that the result doesn't depend on a number of items.

It is because any code after a `yield` command is postponed until you enumerate the output values. If you put a slow sleep command after a `yield` line then you will see no difference because it run never. Even a generator expression command `%timeit (x for x in range(1000000000))` is similarly fast as the same for `range(1)`.

A correct measurement is by `%timeit list(some_function(l))` or `%timeit for _ in some_function(l): pass`

The fastest code for flattening is `flatten_chain(l)` because the expression `itertools.chain(*lst)` uses only a library function call and no Python loops.

[Miguel Brito](https://hashnode.com/@miguendes)

[Dec 18, 2020](https://hashnode.com/posts/python-flatten-list/5fd49556b6e5766805757aa7/comment/5fdc3422b78ac62dff80aa9a#reply-5fdc6aff1485f22e24b33250)

You're absolutely correct. I missed that. So much so that for the tests I always convert it to list.

I'll edit the article with the correct timings.

Thanks for pointing out!

Edit: Fixed it!
