-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_flatten.py
60 lines (58 loc) · 1.29 KB
/
04_flatten.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# %% [markdown]
# # flatten a list
"""
all the ways to flatten a list
Spoiler : use chain if you want to go fast
"""
# %%
# # experiments
from itertools import chain
import functools
import operator
# %%
def create_sublist(i, max_val):
sub = []
for j in range(max_val):
sub.append(f"s{i}_{j:03d}")
return sub
# %%
mysmalllist = [create_sublist(i,3) for i in range(3)]
# %%
"""
All the methods below give the same result
"""
# %%
[item for sublist in mysmalllist for item in sublist]
# %%
sum([sublist for sublist in mysmalllist], [])
# %%
list(chain(*(sublist for sublist in mysmalllist)))
# %%
list(chain.from_iterable(sublist for sublist in mysmalllist))
# %%
functools.reduce(operator.iconcat, mysmalllist, [])
# %%
"""
Timing
"""
# %%
nb_elem_per_sublist = 100
nb_sublists = 100
mylist = [create_sublist(i,nb_elem_per_sublist) for i in range(nb_sublists)]
# %%
%%timeit
[item for sublist in mylist for item in sublist] # almost as fast as chain if nb of sublist is small
# %%
%%timeit
sum([sublist for sublist in mylist], [])
# %%
%%timeit
list(chain(*(sublist for sublist in mylist)))
# %%
%%timeit
list(chain.from_iterable(sublist for sublist in mylist))
# %%
%%timeit
functools.reduce(operator.iconcat, mylist, [])
# %%
# https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists