faster than list.extend()

Chris Rebert clp2 at rebertia.com
Mon Nov 16 17:41:25 EST 2009


On Mon, Nov 16, 2009 at 2:30 PM, Hyunchul Kim
<hyunchul.mailing at gmail.com> wrote:
> Hi, all.
>
> I want to improve speed of following simple function.
> Any suggestion?
>
> **********
> def triple(inputlist):
>   results = []
>   for x in inputlist:
>     results.extend([x,x,x])
>   return results
> **********

You'd probably be better off calling .append() 3 times instead of
.extend() as it would avoid the creation of all those intermediate
3-element lists.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list