How does the list comprehension to flatten a python list work?

后端 未结 5 1450
忘了有多久
忘了有多久 2020-12-30 06:50

I recently looked for a way to flatten a nested python list, like this: [[1,2,3],[4,5,6]], into this: [1,2,3,4,5,6].

Stackoverflow was helpful as ever and I found a

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 07:28

    While this approach definitely works for flattening lists, I wouldn't recommend it unless your sublists are known to be very small (1 or 2 elements each).

    I've done a bit of profiling with timeit and found that this takes roughly 2-3 times longer than using a single loop and calling extend…

    def flatten(l):
        flattened = []
        for sublist in l:
            flattened.extend(sublist)
        return flattened
    

    While it's not as pretty, the speedup is significant. I suppose this works so well because extend can more efficiently copy the whole sublist at once instead of copying each element, one at a time. I would recommend using extend if you know your sublists are medium-to-large in size. The larger the sublist, the bigger the speedup.

    One final caveat: obviously, this only holds true if you need to eagerly form this flattened list. Perhaps you'll be sorting it later, for example. If you're ultimately going to just loop through the list as-is, this will not be any better than using the nested loops approach outlined by others. But for that use case, you want to return a generator instead of a list for the added benefit of laziness…

    def flatten(l):
        return (item for sublist in l for item in sublist) # note the parens
    

提交回复
热议问题