less list creation.<br><br><div class="gmail_quote">On Thu, Feb 5, 2009 at 10:17 PM, mk <span dir="ltr"><<a href="mailto:mrkafk@gmail.com">mrkafk@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">Brian Allen Vanderburg II wrote:<br>
>> def flatten(x):<br>
>> res = []<br>
>> for el in x:<br>
>> if isinstance(el,list):<br>
>> res.extend(flatten(el))<br>
>> else:<br>
>> res.append(el)<br>
>> return res<br>
<br>
><br></div><div class="Ih2E3d">
> I think it may be just a 'little' more efficient to do this:<br>
><br>
> def flatten(x, res=None):<br>
> if res is None:<br>
> res = []<br>
><br>
> for el in x:<br>
> if isinstance(el, (tuple, list)):<br>
> flatten(el, res)<br>
> else:<br>
> res.append(el)<br>
><br>
> return res<br>
<br>
<br></div>
Hmm why should it be more efficient? extend operation should not be very<br>
costly?<div><div></div><div class="Wj3C7c"><br>
<br>
Regards,<br>
mk<br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>