help understanding class or function

Rhodri James rhodri at wildebst.demon.co.uk
Thu Mar 5 20:51:24 EST 2009


Please don't top-post.

On Fri, 06 Mar 2009 01:09:48 -0000, Vincent Davis  
<vincent at vincentdavis.net> wrote:

> I guess I am thinking of it as an operation that I am preforming on a  
> list
> and not making a new list. Also looking to learn better methods. This is
> difficult when you are working alone. It is difficult to know where to
> improve.
> The actual functions I am making are more complicated than this example.

Whether you want to change the list in-place or not will depend on several
things.  Most importantly, it will depend on your data usage.  If your
list crops up in several places and you want the changes you're about to
make to propagate to them all, then you must change the list in-place
(possibly using the [:] slicing trick).  If you don't want the changes
to be reflected elsewhere, you must build a new list instead.

If you don't care (which is the normal case, hopefully), then it depends
on what your function is supposed to be doing.  If it's changing every
element of the list, doing the changes in-place should be a doddle.  If
it's filtering some elements out (like your example of omitting every
other element) then it's much easier to build a new list.

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list