Flattening lists
Brian Allen Vanderburg II
BrianVanderburg2 at aim.com
Thu Feb 5 11:10:51 EST 2009
mrkafk at gmail.com wrote:
> Baolong zhen wrote:
>> less list creation.
>
> At the cost of doing this at each 'flatten' call:
>
> if res is None:
> res = []
>
> The number of situations of executing above code is the same as the
> number of list creations (once for each 'flatten' call, obviously).
>
> Is list creation really more costly than above?
>
Probably not. I wrote a small test program using a list several levels
deep, each list containing 5 sublists at each level and finally just a
list of numbers. Flattening 1000 times took about 3.9 seconds for the
one creating a list at each level, and 3.2 for the one not creating the
list at each level.
Brian Vanderburg II
More information about the Python-list
mailing list