Vincent Davis 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.
python occupies a strange place. in some ways it encourages a style of programming that takes ideas from functional programming, and at other times from object oriented programming. here you are drifting towards a more functional approach. in such languages it's often quite normal to return new lists, and for the pattern a = somefunc(a) to occur. this seems a bit odd if you're used to objects modifying themselves in-place, or concerned with performance. but it's not necessarily bad style in python. this may be a somewhat personal viewpoint - just throwing it out in case it helps. you might find looking at ML or Scheme or Erlang useful later... andrew > The actual functions I am making are more complicated than this example. > Thanks > Vincent Davis > 720-301-3003 > > > On Thu, Mar 5, 2009 at 5:13 PM, Gabriel Genellina > <gagsl-...@yahoo.com.ar>wrote: > >> En Thu, 05 Mar 2009 14:43:12 -0200, Vincent Davis < >> vinc...@vincentdavis.net> escribió: >> >> >> If I have a list and I what to make changes to it.a = >> [1,2,3,4,5,6,7,8,9] >>> and maybe I want to drop the odd and double the even numbers and I >>> will >>> need to do this repeatedly. >>> How is this best done? That is as a function or class. All I know how >>> to >>> do >>> is >>> >>> def doubleeven(alist): >>> blist = [2*x for x in a if x % 2 ==0] >>> return blist >>> >>> then when I am using it I do this >>> >>> a = doubleeven(a) >>> I what to keep it named "a" >>> >>> I am not sure if this is the best way in terms of format or structure. >>> Is >>> there a better way. >>> >> >> I don't grasp what you're worried about... Your function does exactly >> what >> you have specified above, so it's correct. Don't you like the fact that >> it >> returns a *new* list instead of modifying the original one in-place? You >> may >> use: >> >> a[:] = doubleeven(a) >> >> Or rewrite the function using a while loop (to avoid having two lists >> alive >> at the same time), but I would not bother unless there are strong >> reasons >> (the lists is really huge, or there is very few memory available). >> >> -- >> Gabriel Genellina >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list