35. Flattening data

Here, we need to flatten an array of arrays into one array. Notice that the second discouraged approach is actually the fastest (faster than the encouraged approaches)? The setup with the x array and use of a for loop spans 3 lines. This example appears controversial with trading off idiomatic Python for speed.

35.1. Don’t do this

1data = [list(range(10000000)) for _ in range(10)]
2
3x = []
4for arr in data:
5    for val in arr:
6        x.append(val)
1data = [list(range(10000000)) for _ in range(10)]
2
3x = []
4for arr in data:
5    x.extend(arr)

35.2. Do this

1data = [list(range(10000000)) for _ in range(10)]
2
3x = [val for arr in data for val in arr]
1import itertools
2
3data = [list(range(10000000)) for _ in range(10)]
4
5x = itertools.chain.from_iterable(data)