You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
777 B
777 B
layout | status | published | title | author | date | categories | tags |
---|---|---|---|---|---|---|---|
post | publish | true | Python: Performance Benchmark | [{display_name sipp11}] | 2013-11-20 | [coding] | python benchmark |
`[item for sublist in l for item in sublist] is faster than the shortcuts posted so far.
For evidence, as always, you can use the timeit module in the standard library:
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x, y: x+y,l)'
1000 loops, best of 3: 1.1 msec per loop